Skip to the code
cf.
+25 XP

#457 Merge Customer Profiles by Email

medium

A CRM imported customer profiles from several shops. Each profile is an array whose first item is the customer's name and whose other items are email addresses. Two profiles belong to the same customer if they share at least one email, and this is transitive. Profiles of the same customer always have the same name.

Write mergeProfiles(profiles) that returns one entry per customer: the name followed by all of that customer's distinct emails sorted in plain string order (the order sort() uses). Sort the entries by their first email, in the same order.

Example 1

Input: profiles = [["Ana","ana@shop.io","a.k@mail.io"],["Ben","ben@shop.io"],["Ana","a.k@mail.io","ana.k@work.io"]]
Output: [["Ana","a.k@mail.io","ana.k@work.io","ana@shop.io"],["Ben","ben@shop.io"]]

The two Ana profiles share a.k@mail.io, so they merge. "." sorts before "@", so ana.k@work.io comes before ana@shop.io.

Example 2

Input: profiles = [["Kai","kai@a.io"],["Kai","kai@b.io"]]
Output: [["Kai","kai@a.io"],["Kai","kai@b.io"]]

Same name but no shared email, so they stay two customers.

Constraints

  • 1 <= profiles.length <= 1000
  • Each profile has a name and 1 to 10 emails.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).