Accounts Merge
Merge accounts that share at least one email into a single account per person, since the same person may have multiple listed accounts.
Open on LeetCode ↗Intuition
The trap is merging on the person's NAME. Two different people can easily share a name, so name is never a safe key for identity -- the real identity is the EMAIL. Union every pair of emails that co-occur inside one account, since owning the same account proves they belong to the same person. After all unions, group every email by its Union-Find root: each group is one real person's full email set. The name is only a label attached at the very end, read off any account that happened to own one of the emails in that group -- it plays no role in the merging logic itself.
Approach
Union emails within each account
For each account, treat its first email as the anchor and union every other email in that account with it. This links all emails an account claims, regardless of the account's name.
Group by root, not by name
After processing every account, find() every email to get its root, and bucket all emails sharing a root together -- that bucket is exactly one person's merged emails, even if it came from several differently-named accounts.
Attach a name only for output
Pick the name from any account that owned one email in the group (they must agree it's the same person's data) and prepend it to the sorted email list -- purely cosmetic, never used for merging.
Solution & live demo
Edge cases
Their emails never got unioned (no shared email), so they remain separate groups despite the same name.
All emails end up under one root via chained unions through the common email.
Stays its own singleton group.
Sort each group's email list before prepending the name, per the problem's required output format.