Deviopath › TypeScript

Friend Groups with Union-Find

Medium · Graphs

Write friendGroups(names, links): every link [a, b] means a and b are friends, and friendship is transitive (a friend of a friend is in the same group). Return all groups, including people with no friends.

Ordering: sort the names inside each group alphabetically, then sort the groups by their first name.

Suggested approach: fill in the generic UnionFind<T> (disjoint set) from the starter, with add, find (with path compression) and union (attach the smaller set under the larger one). Any other correct approach is fine too.

Next: Undo / Redo History