cardinality
Description
Returns the number of unique elements in an array.
- The element type must match the array type.
- Supported element types include
string,bool,number,interval,timestamp,regexp, andenum.
Syntax
Like many functions in DataPrime, cardinality supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.
- Function notation
- Method notation
cardinality(array: array<T>): number
(array: array<T>).cardinality(): number
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
array | array<T> | true | The array whose unique elements will be counted |
Example
Use case: Detect the number of unique IP addresses
Suppose you have a list of active IPs in a system. Consider the following input:
{
"active_ips": ["123.45.32.1", "111.230.76.5", "9.8.7.1", "123.45.32.1"]
}
By applying cardinality, you can determine how many unique IP addresses are present in the array.
Example query
- Function notation
- Method notation
create unique_ip_count from cardinality(active_ips)
create unique_ip_count from active_ips.cardinality()
Example output
The result will include a new field unique_ip_count showing the number of unique elements:
{
"active_ips": ["123.45.32.1", "111.230.76.5", "9.8.7.1", "123.45.32.1"],
"unique_ip_count": 3
}