# `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://coralogix.com/docs/dataprime/language-reference/functions-reference/index.md), **function** and **method** notation. These interchangeable forms allow flexibility in how you structure expressions.

```dataprime
cardinality(array: array<T>): number
```

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

## Arguments

| Name  | Type  | Required | Description                                     |
| ----- | ----- | -------- | ----------------------------------------------- |
| array | array | **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:

```json
{
    "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

```dataprime
create unique_ip_count from cardinality(active_ips)
```

```dataprime
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:

```json
{
    "active_ips": ["123.45.32.1", "111.230.76.5", "9.8.7.1", "123.45.32.1"],
    "unique_ip_count": 3
}
```
