196. Delete Duplicate Emails
Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 45.9%
Problem Breakdown
The problem requires us to delete duplicate email entries from a table named Person. The table has two columns: Id (an integer) and Email (a string). Our goal is to remove all rows that have duplicate emails, keeping only one row for each unique email. The input is the Person table itself, and the output should be the modified table with duplicates removed.
Inputs:
- A table
Personwith columnsIdandEmail.
Outputs:
- The same table
Personwith duplicate emails removed, keeping only one row per unique email.
Implicit Constraints/Edge Cases:
- If the table is empty, there are no duplicates to remove.
- Emails are case-sensitive; “example@domain.com” and “Example@Domain.com” are considered different.
- The order of rows in the output does not matter.
Intuition
A brute-force approach would involve checking each row against every other row to identify duplicates. This would be inefficient with a time complexity of O(n^2), where n is the number of rows in the table. A better approach involves using a set to track seen emails, which allows us to efficiently check for duplicates as we iterate through the table.
The key insight is that by leveraging a set to store unique emails, we can achieve linear time complexity, making our solution much more efficient.
Approach
- Initialize a Set: Create an empty set called
seen_emailsto keep track of emails that have already been encountered. - Iterate Through the Table: Loop through each row in the
Persontable. - Check for Duplicates:
- If the email is not in the
seen_emailsset, add it to the set and proceed to the next row. - If the email is already in the set, delete that row from the table.
- If the email is not in the
- Return the Modified Table: After processing all rows, return the modified
Persontable with duplicates removed.
Complexity Analysis
- Time Complexity: O(n), where n is the number of rows in the
Persontable. We iterate through each row once and perform constant-time operations for set lookups and deletions. - Space Complexity: O(m), where m is the number of unique emails. In the worst case, all emails are unique, so we store all email addresses in the set.
Solution
def delete_duplicate_emails(person_table):
seen_emails = set()
# Iterate through a copy of the table to avoid modifying it while iterating
for row in person_table.copy():
if row['Email'] not in seen_emails:
seen_emails.add(row['Email'])
else:
person_table.remove(row)
return person_table
Edge Cases Handled
- Empty Table: If the
Persontable is empty, the function will simply return an empty list without any modifications. - All Unique Emails: If all emails in the table are unique, the set
seen_emailswill contain all email addresses, and no rows will be deleted. - Case-Sensitive Emails: The solution treats emails as case-sensitive, so “example@domain.com” and “Example@Domain.com” are considered different and both can exist in the table.