182. Duplicate Emails
Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 65.0%
Problem Breakdown
The problem asks us to find duplicate email addresses from a table named Person. The table has two columns: Id (an integer) and Email (a string). We need to return the list of emails that appear more than once in the table.
Inputs:
- A table
Personwith columnsIdandEmail.
Outputs:
- A list of duplicate email addresses.
Implicit Constraints or Tricky Edge Cases:
- The input table might be empty.
- Emails are case-sensitive, so “example@domain.com” and “Example@Domain.com” are considered different.
- There could be multiple duplicates of the same email address.
Intuition
To solve this problem, we can start with a brute-force approach where we iterate through each row in the table, check if the email already exists in a list or set, and add it to our result if it does. However, this approach would have a time complexity of O(n^2) because for each email, we might need to scan the entire list to check for duplicates.
A better approach is to use a hash map (or dictionary) to keep track of the count of each email as we iterate through the table. This way, we can achieve a linear time complexity of O(n), where n is the number of rows in the table.
Approach
- Initialize an empty dictionary to store the count of each email.
- Iterate through each row in the
Persontable. - For each email, increment its count in the dictionary.
- After processing all rows, iterate through the dictionary and collect emails that have a count greater than 1.
- Return the list of duplicate emails.
Complexity Analysis
- Time Complexity: O(n), where n is the number of rows in the
Persontable. We make a single pass to populate the dictionary and another pass to collect duplicates. - Space Complexity: O(n) in the worst case, where all emails are unique and stored in the dictionary.
Solution
def find_duplicate_emails(person):
email_count = {}
# Count occurrences of each email
for row in person:
email = row['Email']
if email in email_count:
email_count[email] += 1
else:
email_count[email] = 1
# Collect emails with more than one occurrence
duplicates = [email for email, count in email_count.items() if count > 1]
return duplicates
Edge Cases Handled
- Empty Input: If the input table is empty, the function will return an empty list.
- Case Sensitivity: The solution treats emails as case-sensitive, so different cases are considered distinct.
- Multiple Duplicates: Emails that appear more than twice are correctly identified and included in the result.