Union-find (disjoint set)0/59 done
Part 1 · Concepts · Graphs

Union-find (disjoint set)

Tracks which items are in the same group, with near O(1) merge and same-group checks.

How it works

Every item points to a parent, and the root names the group. find walks up to the root and flattens the path as it goes (path compression). union links one root under the other, attaching the shorter tree under the taller (union by rank). Together these make operations effectively constant time.

find / unionabout O(1) amortized (inverse Ackermann)
spaceO(n)

Reach for it when

Implementation (JavaScript, tested)
class UnionFind {
  parent;
  rank;
  count; // number of separate groups
  constructor(n) {
    this.parent = Array.from({ length: n }, (_, i) => i);
    this.rank = new Array(n).fill(0);
    this.count = n;
  }
  find(x) {
    while (this.parent[x] !== x) {
      this.parent[x] = this.parent[this.parent[x]]; // path halving: flatten as we climb
      x = this.parent[x];
    }
    return x;
  }
  union(a, b) {
    let ra = this.find(a),
      rb = this.find(b);
    if (ra === rb) return false; // already together (in a graph: a cycle)
    if (this.rank[ra] < this.rank[rb]) [ra, rb] = [rb, ra];
    this.parent[rb] = ra; // hang the shorter tree under the taller
    if (this.rank[ra] === this.rank[rb]) this.rank[ra]++;
    this.count--;
    return true;
  }
}

Traps

In real systems: Merging duplicate customer records or accounts that share a phone or email, clustering, image region labeling.
Say out loud: If I only need 'are these connected' while merging groups, union-find is simpler than running BFS each time.