Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Farray%2Fcardinality.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Farray%2Fcardinality.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# `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](https://docs-docusaurus.kinsta.page/dataprime/language-reference/functions-reference/.md),<!-- --> **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

}
```
