Goal
Implement first_duplicate(nums) to return the first element that appears twice in the list. If no element is duplicated, return None.
Requirements
- Use a set to track seen elements for efficient lookup.
- Return the first duplicate you encounter as you iterate through the list.
- If there are no duplicates, return
None.
Example
first_duplicate([1, 2, 2, 3]) → 2
first_duplicate([1, 2, 3, 4]) → None