#182

Duplicate Emails

Easy
DatabaseHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

Using a hash map allows us to count occurrences of each email efficiently. This way, we can find duplicates in a single pass through the data.

⚙️

Algorithm

3 steps
  1. 1Step 1: Initialize a hash map to count occurrences of each email.
  2. 2Step 2: Loop through each email and update the count in the hash map.
  3. 3Step 3: After counting, filter the hash map to find emails with a count greater than 1.
solution.py2 lines
1# Full working Python code
2SELECT email FROM (SELECT email, COUNT(email) as count FROM Person GROUP BY email) as counts WHERE count > 1;

Complexity note: The time complexity is O(n) because we only pass through the list of emails once to count them. The space complexity is O(n) due to the hash map storing counts for each unique email.

  • 1Using a hash map can significantly reduce time complexity.
  • 2Grouping and counting can be efficiently handled with SQL aggregate functions.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.