# PFADD

> Add elements to a HyperLogLog for approximate distinct counting.

URL: https://docs.hotkv.com/commands/pfadd/

```
PFADD key [element [element ...]]
```

- Group: HyperLogLog
- Complexity: O(1) per element
- HotKV Cloud: all plans
- On-Premise: no license needed

`PFADD` adds elements to the HyperLogLog at `key`, creating it if needed. A
HyperLogLog estimates how many distinct elements it has seen, using a small,
fixed amount of memory (at most about 12 KB) whatever the count, with a
typical error below 1%. Use it to count unique visitors, search terms or
events when an exact set would be too large.

Called with no elements, `PFADD` only creates an empty HyperLogLog if the key
does not exist. HyperLogLogs are stored as strings, so [TYPE](https://docs.hotkv.com/commands/type/index.md)
reports `string`. The key keeps its time to live.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| The estimate changed, or the key was created | Integer `1` | Integer `1` |
| Nothing changed | Integer `0` | Integer `0` |

Errors:

- `WRONGTYPE Key is not a valid HyperLogLog string value.` when the key holds a
  string that is not a HyperLogLog.
- `INVALIDOBJ Corrupted HLL object detected` when the HyperLogLog data is
  damaged.
- `WRONGTYPE Operation against a key holding the wrong kind of value` when the
  key holds another type.

## Examples

```
127.0.0.1:6379> PFADD visitors alice bob carol
(integer) 1
127.0.0.1:6379> PFADD visitors alice
(integer) 0
127.0.0.1:6379> PFCOUNT visitors
(integer) 3
```

## Compatibility

Same as Redis 7 and Valkey 8, and the stored format is the same: a HyperLogLog
moved between HotKV and Redis with [DUMP](https://docs.hotkv.com/commands/dump/index.md) and
[RESTORE](https://docs.hotkv.com/commands/restore/index.md) keeps working.

## See also

[PFCOUNT](https://docs.hotkv.com/commands/pfcount/index.md), [PFMERGE](https://docs.hotkv.com/commands/pfmerge/index.md),
[PFDEBUG](https://docs.hotkv.com/commands/pfdebug/index.md)