SDIFFCARD

SDIFFCARD numkeys key [key ...] [LIMIT limit]
Available since:
Redis Open Source 8.10.0
Time complexity:
O(N) where N is the total number of elements in all given sets.
ACL categories:
@read, @set, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility
Note:
This command's behavior varies in clustered Redis environments. See the multi-key operations page for more information.

Returns the cardinality of the difference between the first set and all the successive sets. This is the count-only counterpart of SDIFF: it returns just the number of elements in the difference, not the members themselves.

Required arguments

numkeys

The number of keys that follow.

key [key ...]

One or more set keys. The result counts the members of the first set that are not present in any of the subsequent sets.

Optional arguments

LIMIT limit

Stop counting once the cardinality reaches limit. 0 (the default) means no limit.

Examples

> SADD key1 "a"
(integer) 1
> SADD key1 "b"
(integer) 1
> SADD key1 "c"
(integer) 1
> SADD key2 "c"
(integer) 1
> SADD key2 "d"
(integer) 1
> SADD key2 "e"
(integer) 1
> SDIFF key1 key2
1) "b"
2) "a"
> SDIFFCARD 2 key1 key2
(integer) 2
> SDIFFCARD 2 key1 key2 LIMIT 1
(integer) 1

Details

Keys that do not exist are considered to be empty sets.

When provided with the optional LIMIT argument (which defaults to 0, meaning unlimited), if the difference cardinality reaches limit partway through the computation, the command stops and returns limit as the cardinality. This ensures a significant speedup for queries where the limit is lower than the actual difference cardinality.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
❌ Standard
❌ Active-Active
❌ Standard
❌ Active-Active

Return information

Integer reply: the number of elements in the resulting difference.

See also

SDIFF | SDIFFSTORE

RATE THIS PAGE
Back to top ↑