LeetCode 1971 Find if Path Exists - Solution

The pattern

Use Union-Find. Two nodes have a path between them if and only if they're in the same connected component.

Process all edges. For each edge [u, v], union the sets containing u and v.

After processing all edges, check if find(source) == find(destination).

Union-Find with path compression and union by rank gives near-constant time per operation.