When to Use

Pattern triggers

Look for these signals in the problem:

  • "Are X and Y connected": queries after union operations: Union Find answers connectivity in near O(1)O(1).
  • "Number of connected components": Count distinct roots after processing all edges.
  • "Detect cycle" in undirected graph: If union finds same root, edge creates cycle.
  • Dynamic connectivity: When edges are added over time and you need connectivity queries throughout.
  • "Group" or "merge" accounts/elements: When merging sets based on shared properties.