Data Structures19 sections · 729 units
Open in Course

Problem - Accounts Merge

Grouping by shared elements

Given a list of accounts where each account is [name, email1, email2, ...], merge accounts belonging to the same person.

Two accounts belong to the same person if they share any common email. Return merged accounts with sorted unique emails.

Example:

accounts = [["John","j1@m.co","j2@m.co"],
            ["John","j3@m.co"],
            ["John","j1@m.co","j4@m.co"]]
Output: [["John","j1@m.co","j2@m.co","j4@m.co"],
         ["John","j3@m.co"]]

Accounts 0 and 2 merge (share j1@m.co). Account 1 is separate.

Constraints: up to 10001000 accounts.