Program to find number of unique people from list of contact mail ids in Python

Suppose we have a list of mail ids in a constants list. So for each row there may be more than one mail id of same person. The contact i is considered as duplicate when there's any j, where j < i such that contact j shares a common email with i. So we have to find the number of unique people in contacts.

So, if the input is like contacts = [["", ""], ["", ""], [""] ], then the output will be 2, as the first and second contacts are sharing same mail ids, so they are same person, so there are two unique people.

To solve this, we will follow these steps −

  • ans := 0
  • found := a new set
  • for each c in contacts, do
    • dullicate := False
    • for each email in c, do
      • if email is not found, then
        • mark email as found
      • otherwise,
        • dullicate := True
    • if dullicate is False, then
      • ans := ans + 1
  • return ans


Let us see the following implementation to get better understanding −

def solve(contacts):
   ans = 0
   found = set()

   for c in contacts:
      dullicate = False

      for email in c:
         if email not in found:
            dullicate = True
      if not dullicate:
         ans += 1

   return ans

contacts = [
["", ""],
["", ""],


[["", ""],
["", ""],



Updated on: 14-Oct-2021


Kickstart Your Career

Get certified by completing the course

Get Started