Delete Duplicate Emails — LeetCode 196 Python Solution
EasyDatabase
- Problem
- #196
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Delete the duplicate rows so that only the row with the smallest id survives for each email address, leaving every other row untouched. The Person table has id (int, primary key) and email (varchar), and the addresses never contain uppercase letters. The answer changes the table in place rather than returning a result set.
Example
- Input
- Person(id, email) = (1, 'ana@example.com'), (2, 'ben@example.com'), (3, 'ana@example.com'), (4, 'ana@example.com')
- Output
- Person(id, email) = (1, 'ana@example.com'), (2, 'ben@example.com')
- Explanation
- Ids 3 and 4 duplicate an address already held by id 1, so only the smallest id per address survives.
Python solution
Python
import pandas as pd
# Modify Person in place
def delete_duplicate_emails(person: pd.DataFrame) -> None:
# Sort the rows based on id (Ascending order)
person.sort_values(by="id", ascending=True, inplace=True)
# Drop the duplicates based on email.
person.drop_duplicates(subset="email", keep="first", inplace=True)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 196. Delete Duplicate Emails?
- LeetCode 196. Delete Duplicate Emails is rated Easy on LeetCode.
- What topics does LeetCode 196. Delete Duplicate Emails cover?
- LeetCode 196. Delete Duplicate Emails is tagged Database on LeetCode.