- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 = [["alex@gmail.com", "alex@yahoo.com"], ["alex_25@yahoo.com", "alex@gmail.com"], ["bob15@gmail.com"] ], 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 email is not found, then
- if dullicate is False, then
- ans := ans + 1
- return ans
Example
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: found.add(email) else: dullicate = True if not dullicate: ans += 1 return ans contacts = [ ["alex@gmail.com", "alex@yahoo.com"], ["alex_25@yahoo.com", "alex@gmail.com"], ["bob15@gmail.com"] ] print(solve(contacts))
Input
[["alex@gmail.com", "alex@yahoo.com"], ["alex_25@yahoo.com", "alex@gmail.com"], ["bob15@gmail.com"] ]
Output
2
Advertisements