Reputation: 4789
I am working on a project which requires to find similar contact.
I/P : it's in the form
C1 -> [email protected],[email protected] ... (could be any size) C2 -> [email protected] C2 -> [email protected],[email protected] C4 -> [email protected],[email protected]
I want to group contacts like C2,C4 and merge them together because they share same email.
Upvotes: 2
Views: 672
Reputation: 3443
Hash the Addresses to the contacts, and when you encounter an address that is already in the hashmap, that's a shared contact.
it is roughly O(N)
Edit:
in case there are more then two candidates with matching emails, they all are grouped.
e.g.
C1 [email protected]
C1 [email protected]
C2 [email protected]
C2 [email protected]
C3 [email protected]
C3 [email protected]
1st Pass:
[email protected] => C1
[email protected] => C1
+ [email protected] => C2 (ALREADY C1) => merge C1 -> C2
[email protected] => C2
[email protected] => C3 (Already C1) => Merge C1 -> C3
[email protected] => C3
now you know you need to merge: C2 into C1, and C3 into C1.
Upvotes: 2
Reputation: 20403
I would try to apply Union Find algorithm.
Merge the containers (i.e. union) whenever they have a common email address.
See: https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf
Upvotes: 0