EasyNo. 0182

Duplicate Emails — LeetCode 182 Python Solution

Topics
Database
Reading time
2 min

The problem

Report every email address that is recorded more than once. The Person table has id (int, primary key) and email (varchar), one row per record, and the addresses never contain uppercase letters. Each duplicated address should be listed once, in a column named Email.

Example

Input
Person(id, email) = (1, 'ana@example.com'), (2, 'cleo@example.com'), (3, 'ana@example.com'), (4, 'ben@example.com'), (5, 'ben@example.com')
Output
Email = 'ana@example.com', 'ben@example.com'
Explanation
Both addresses occur twice, so each is reported once; cleo@example.com occurs once and is excluded.

Python solution

PythonSolution
import pandas as pd


def duplicate_emails(person: pd.DataFrame) -> pd.DataFrame:
    results = pd.DataFrame()

    results = person.loc[person.duplicated(subset=["email"]), ["email"]]

    return results.drop_duplicates()

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Frequently asked questions

How hard is LeetCode 182. Duplicate Emails?

LeetCode 182. Duplicate Emails is rated Easy on LeetCode.

What topics does LeetCode 182. Duplicate Emails cover?

LeetCode 182. Duplicate Emails is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.