A site has a list of accounts. Each account is an array that starts with a person's name, followed by one or more email addresses. For example: ["Ana", "ana@work.io", "ana@home.io"].
If two accounts list the same email, they are the same person. This links through chains too. Say A shares an email with B, and B shares one with C. Then A, B and C are one person, even if A and C have no email in common. A shared name proves nothing, though. Two accounts with the same name and no chain of shared emails are different people.
Return one entry per person, in the form [name, ...emails]:
- The emails are that person's emails from all their accounts. List each email once. Sort them in ascending order with plain string comparison. This is what
Array.prototype.sort()does when you give it no compare function. - Sort the entries by their first email, with the same comparison.
- Accounts that share an email always have the same name.
- Every account has a name and at least one email. There are up to 10,000 accounts and up to 100,000 emails in total.
- An empty list of accounts returns
[].
Input: accounts = [["Ana", "ana@work.io", "ana@home.io"], ["Ben", "ben@mail.io"], ["Ana", "ana@home.io", "a.silva@uni.edu"], ["Ana", "ana99@game.net"]]
Output: [["Ana", "a.silva@uni.edu", "ana@home.io", "ana@work.io"], ["Ana", "ana99@game.net"], ["Ben", "ben@mail.io"]]Accounts 0 and 2 share ana@home.io, so they merge. Account 3 is also named Ana, but it shares no email. So it is a different person.
Input: accounts = [["Kai", "k1@x.io"], ["Kai", "k2@x.io", "k3@x.io"], ["Kai", "k3@x.io", "k1@x.io"]]
Output: [["Kai", "k1@x.io", "k2@x.io", "k3@x.io"]]Accounts 0 and 1 share no email. But account 2 shares an email with each of them, so all three are one person.