Skip to content

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, and enum.

Syntax

Like many functions in DataPrime, cardinality supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

cardinality(array: array<T>): number
(array: array<T>).cardinality(): number

Arguments

NameTypeRequiredDescription
arrayarraytrueThe 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.

create unique_ip_count from cardinality(active_ips)
create unique_ip_count from active_ips.cardinality()

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
}