Accounts Merge - Problem

Imagine you're working at a tech company where employees can have multiple email accounts, but sometimes they create duplicate profiles in your system. Your task is to merge accounts that belong to the same person by identifying shared email addresses.

Given a list of accounts where each account contains:

  • First element: name (string)
  • Remaining elements: emails associated with that account

The Challenge: Two accounts belong to the same person if they share at least one email address. Even accounts with the same name might belong to different people, but accounts belonging to the same person will always have the same name.

Your Goal: Return merged accounts where:

  • First element is the person's name
  • Remaining elements are all their emails in sorted order
  • No duplicate emails within each account

Example:
[["John", "a@gmail.com", "b@gmail.com"], ["John", "c@gmail.com"], ["John", "a@gmail.com", "d@gmail.com"]]
[["John", "a@gmail.com", "b@gmail.com", "c@gmail.com", "d@gmail.com"]]

The first and third accounts share a@gmail.com, so all three accounts belong to the same John!

Input & Output

Basic Account Merging
$ Input: accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
Output: [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
💡 Note: The first and second John accounts share 'johnsmith@mail.com', so they merge. The third John has no shared emails with others, so stays separate. Mary remains unchanged.
Transitive Merging
$ Input: accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]]
Output: [["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"]]
💡 Note: No accounts share emails, so each person remains as a separate account with their emails sorted.
Single Account
$ Input: accounts = [["David","David0@m.co","David1@m.co"]]
Output: [["David","David0@m.co","David1@m.co"]]
💡 Note: Edge case with only one account - return it with emails in sorted order.

Constraints

  • 1 ≤ accounts.length ≤ 1000
  • 2 ≤ accounts[i].length ≤ 10
  • 1 ≤ accounts[i][j].length ≤ 30
  • accounts[i][0] consists of English letters
  • accounts[i][j] (for j > 0) is a valid email address
  • All emails within the same account are unique

Visualization

Tap to expand
Accounts Merge Problem INPUT Account 0: "John" johnsmith@mail.com john_newyork@mail.com Account 1: "John" johnsmith@mail.com john00@mail.com Account 2: "Mary" mary@mail.com Account 3: "John" johnnybravo@mail.com Shared Email Connection: Acc0 Acc1 Acc2 Acc3 shared ALGORITHM STEPS 1 Build Union-Find Init parent[] for each account 2 Map Email to Account Track first occurrence of email 3 Union Shared Emails Connect accounts with same email 4 Group and Sort Collect emails by root, sort them Union-Find Process Before: 0 1 2 3 After union(0,1): 0,1 2 3 3 groups FINAL RESULT Merged Account 1: "John" john00@mail.com john_newyork@mail.com johnsmith@mail.com (sorted alphabetically) Merged Account 2: "Mary" mary@mail.com Merged Account 3: "John" johnnybravo@mail.com Output Array: [ ["John","john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"], ["Mary","mary@mail.com"], ["John","johnnybravo@mail.com"] ] OK - 3 unique persons found Key Insight: Union-Find (Disjoint Set Union) efficiently groups accounts that share emails. When two accounts share an email, we union them into the same set. Time: O(NK * alpha(N)) where N=accounts, K=max emails per account. Path compression makes alpha(N) nearly constant, making this approach optimal for large datasets. TutorialsPoint - Accounts Merge | Optimal Solution (Union-Find)
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28 Apple 15
125.0K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen