Back to Practice
#0308
Find the First Duplicate Element
EasyPython15 min10 XP
Problem
Given a list of hashable values, return the first value whose second occurrence appears earliest. If there is no duplicate, return None.
Why This Matters
Duplicate detection is one of the first useful set patterns: remember what you have already seen.
Function Signature
def first_duplicate(items):
Examples
Example 1
Inputitems = [1, 1, 2, 3]
Output1
The second 1 is the first repeated value encountered.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Prefer clear Python code before clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Immediate duplicate
Input[1, 1, 2, 3]
Expected1
1 appears again before any other value repeats.
Later duplicate
Input["a", "b", "c", "b", "a"]
Expected"b"
b repeats before a repeats.
Hidden Test Categories
no duplicatesempty listduplicate None value