π·οΈΒ Category:Β Lists / HashSet Β |Β π―Β Difficulty:Β EasyΒ Β |Β π₯Β Frequency:Β β
β
β
β
β
π Problem Statement
Given a list of integers, return all elements that appear more than once. This problem is crucial to understand how to Find All Duplicates in a List.
π₯ Input β [1, 2, 3, 2, 4, 3, 5]
π€ Output β [2, 3]
π₯ Input β [4, 3, 2, 7, 8, 2, 3, 1]
π€ Output β [2, 3]
π₯ Input β [1, 2, 3]
π€ Output β []
π§ What the Interviewer is Testing
| Concept | Why It Matters |
|---|---|
| Set for O(1) lookup | Efficient membership testing |
| Tracking seen vs duplicates | Two-set pattern |
| Edge cases | Empty list, all unique |
π‘ Approach
Use TWO sets:
seen β elements we have already encountered
duplicates β elements seen more than once
For each number:
If it's in 'seen' β add to 'duplicates'
Else β add to 'seen'
β Optimal Solution
def find_duplicates(nums: list) -> list:
seen = set()
duplicates = set()
for num in nums:
if num in seen:
duplicates.add(num)
else:
seen.add(num)
return list(duplicates)
# ββ Test Cases βββββββββββββββββββββββββββββββββ
print(find_duplicates([1, 2, 3, 2, 4, 3, 5])) # β
[2, 3]
print(find_duplicates([4, 3, 2, 7, 8, 2, 3, 1])) # β
[2, 3]
print(find_duplicates([1, 2, 3])) # β
[]
print(find_duplicates([])) # β
[]
π Alternative Using Counter
from collections import Counter
def find_duplicates_counter(nums: list) -> list:
return [num for num, count in Counter(nums).items() if count > 1]
π¬ Pro tip: Mention Counter as an alternative β itβs cleaner and shows library knowledge.
π¬ Pro tip: Mention Counter as an alternative β itβs cleaner and shows library knowledge.
π Complexity Analysis
βββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β Time O(n) β Single pass through the list β
β Space O(n) β Two sets in worst case β
βββββββββββββββ΄βββββββββββββββββββββββββββββββββββββ
π€ How to Explain in the Interview
βI use two sets β one to track all seen elements, another to collect duplicates. As I iterate, if a number is already in βseenβ, it goes into βduplicatesβ. This is O(n) time with O(n) space. Alternatively, Counter from collections gives a clean one-liner.β
βI use two sets β one to track all seen elements, another to collect duplicates. As I iterate, if a number is already in βseenβ, it goes into βduplicatesβ. This is O(n) time with O(n) space. Alternatively, Counter from collections gives a clean one-liner.β
π Common Follow-up Questions
Q1 β Return count of each duplicate?
from collections import Counter
def duplicate_counts(nums):
return {k: v for k, v in Counter(nums).items() if v > 1}
# {2: 2, 3: 2}
Q2 β In-place without extra space?
# Only works if nums contains 1 to n (mark visited by negating)
def find_duplicates_inplace(nums):
result = []
for num in nums:
idx = abs(num) - 1
if nums[idx] < 0:
result.append(abs(num))
else:
nums[idx] *= -1
return result
π Set 1 of 4 β Python Interview Prep Series β devprepguide.com
