能有多个email，现在给你an array of Contact，比如
#1 Johny [email@example.com]
#2 Mary [firstname.lastname@example.org]
#3 Johnnathen [email@example.com]
#4 John [firstname.lastname@example.org, email@example.com, firstname.lastname@example.org]
#5 Bob [email@example.com]
This is a classic disjoint set problem. Every user has a (unique) number, and a linked list of emails that associated with the number. We want to group all the contacts that shares same email addresses. (1, 3, and 4 is a group for example).
We can first create a map that maps each email address to a set of users. Then we iterate through the original array, and join those sets which shares same email address.
given an example, if we have
1 -> e1, e2.
2 -> e2, e3,
The hash map will be
e1 -> 1,
e2 -> 1, 2
e3 -> 2,
Then we go through the original list and join the sets under e1 and e2, then e2 and e3, so we will have set 1 and 2.
This can also be solved using a graph like algorithm. we look at the all the name and email addresses as nodes, and the relation ship between them as edges. We can simply form a graph and then run a breath first search and all the names in a connected components will be grouped in one group.