Hard · DSU Identity Merge
Understand → Visualise → Practice.
Each account lists emails. Accounts sharing any email belong together; return the sorted number of distinct emails in each merged group.
Treat accounts as nodes. The first account to list an email owns it; any later account listing the same email is unioned with the owner. Each final set is one person.
Map each email to the first account that lists it.
If an email already has an owner, union the current account with that owner.
Group distinct emails by their owner's root and return the group sizes in ascending order.
O(total emails × α(n)) time and O(total emails) space.
Counting emails per account before merging double-counts shared emails.
Read the full guide first: Union-Find for Dynamic Connectivity.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.