> For the complete documentation index, see [llms.txt](https://emory.gitbook.io/dsa-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emory.gitbook.io/dsa-java/disjoint-sets/concept.md).

# 6.1. Concept

A disjoint set enables to be join sets efficiently.  There are two methods implemented in this structure, `inSameSet()` and `union()`.

In the following example, each key in `{0, 1, 2, 3, 4}` is initially in its own set:

```
0: {0}
1: {1}
2: {2}
3: {3}
4: {4}
```

`inSameSet(1, 3)` checks if the keys `1` and `3` are in the same set:

```java
inSameSet(1, 3) -> false
```

The `union(1, 3)` method joins two sets including `1` and `3` as one set:

```
0: {0}
1: {1, 3}
2: {2}
3: {1, 3}
4: {4}
```

If we check if the keys `1` and `3` are in the same set, it should return `true` although for the keys `1` and `4`, it should return `false`.

```java
inSameSet(1, 3) -> true
inSameSet(1, 4) -> false
```

The `union(3, 4)` method joins the two sets including `3` and `4` as one:

```
0: {0}
1: {1, 3, 4}
2: {2}
3: {1, 3, 4}
4: {1, 3, 4}
```

If we check if `1`, `3` and `4` are in the same set, it should return `true`:

```java
`inSameSet(1, 4)` -> true
`inSameSet(3, 4)` -> true
```
