# SDIFFCARD

> Get set difference cardinality.

Use `SDIFFCARD` to count the members of the first set that are not present in any of the other sets. It returns only the count; use [`SDIFF`](/redis/commands/set/sdiff) to retrieve the members.

Keys that do not exist count as empty sets, so a missing first key returns `0`. The command does not modify the source sets or store the difference.

`LIMIT` stops counting once that many members have been found and returns the limit. For example, `LIMIT 1` checks whether the difference contains any members. Omitting `LIMIT` or using `LIMIT 0` returns the full count.

## Syntax

```redis
SDIFFCARD <numkeys> <key> [<key> ...] [LIMIT <limit>]
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `<numkeys>` | Yes | No | Positive integer specifying how many key arguments follow. |
| `<key>` | Yes | Yes | Set key. Members of all subsequent sets are excluded from the first set. |
| `LIMIT <limit>` | No | No | Non-negative integer that caps the returned count; `0` means no limit. |

## Important points

- `numkeys` must equal the number of key arguments that immediately follow it.
- Key order matters: the command subtracts the other sets from the first set.
- With one key, the command returns that set's cardinality, capped by a positive `LIMIT`.

## Response

The number of members in the difference, capped by `LIMIT` when it is positive.

| Protocol | Reply |
| --- | --- |
| RESP2 | Integer |
| RESP3 | Integer |

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. Client examples query `set1` and `set2` from the Redis CLI example.

<AccordionGroup>

<Accordion title="Redis CLI" icon="terminal">

```redis
> SADD set1 a b c
(integer) 3
> SADD set2 c d e
(integer) 3
> SDIFFCARD 2 set1 set2
(integer) 2
> SDIFFCARD 2 set1 set2 LIMIT 1
(integer) 1
> SDIFFCARD 2 set1 set2 LIMIT 0
(integer) 2
```

The difference contains `a` and `b`, so its cardinality is `2`.

</Accordion>

<Accordion title="@upstash/redis" icon="node-js" iconType="brands">

<Note>
  This command is not supported yet in `@upstash/redis`.
</Note>

</Accordion>

<Accordion title="upstash_redis" icon="python" iconType="brands">

<Note>
  This command is not supported yet in `upstash_redis`.
</Note>

</Accordion>

<Accordion title="ioredis" icon="node-js" iconType="brands">

```ts
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);
const result = await redis.sdiffcard(2, "set1", "set2");
console.log(result);
```

</Accordion>

<Accordion title="node-redis" icon="node-js" iconType="brands">

```ts
import { createClient } from "redis";

const client = await createClient({ url: process.env.REDIS_URL })
  .on("error", console.error)
  .connect();
const result = await client.sDiffCard(["set1", "set2"]);
console.log(result);
```

</Accordion>

<Accordion title="redis-py" icon="python" iconType="brands">

```python
import os
import redis

client = redis.from_url(os.environ["REDIS_URL"])
result = client.sdiffcard(2, ["set1", "set2"])
print(result)
```

</Accordion>

<Accordion title="go-redis" icon="golang" iconType="brands">

```go
package main

import (
    "context"
    "fmt"
    "os"

    "github.com/redis/go-redis/v9"
)

func main() {
    opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
    if err != nil {
        panic(err)
    }
    client := redis.NewClient(opts)
    defer client.Close()
    result, err := client.SDiffCard(context.Background(), nil, "set1", "set2").Result()
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
}
```

</Accordion>

<Accordion title="jedis" icon="java" iconType="brands">

```java
import java.net.URI;

import redis.clients.jedis.Jedis;

try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
  long result = jedis.sdiffcard("set1", "set2");
  System.out.println(result);
}
```

</Accordion>

<Accordion title="redis-rs" icon="rust" iconType="brands">

```rust
fn main() -> redis::RedisResult<()> {
    let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
    let client = redis::Client::open(url)?;
    let mut connection = client.get_connection()?;

    let mut command = redis::cmd("SDIFFCARD");
    command.arg(2).arg("set1").arg("set2");
    let result: i64 = command.query(&mut connection)?;
    println!("{result:?}");
    Ok(())
}
```

</Accordion>

</AccordionGroup>
