First duplicate
Inputs
values is a list of 0 to 14 whole numbers, each from 0 to 12.
Your tasks
- Create an empty list
seen. - Use a loop over
values. If the number is already inseen, return it right away. Otherwise add it toseen. - Complete
firstDuplicateso it returns the first number that appears a second time, or null. Return it, do not print it.
Example
With values = [3, 7, 4, 7, 3], firstDuplicate returns 7.
Rules
- "First" is by the position of the second appearance.
- Return null (None in Python) when all numbers are different.
Test data
TEST DATA 1: values is [3, 7, 4, 7, 3]
TEST DATA 2: values is [1, 2, 3]
Practice focus
While loops and early exits, for when you don't know how many rounds you need. Binary search, the Euclidean algorithm and graph search are all while loops with a clear stopping rule.