A row of lockers was assigned codes in order. Some codes were reused across several lockers. Scanning from the first locker to the last, find the earliest locker whose code appears exactly once in the whole list. Report its 0-indexed position, or -1 if no code is unique.
Line 1: an integer n, the number of lockers.
Line 2: n space-separated integers, the locker codes in order.
A single integer: the 0-indexed position of the first code that occurs exactly once, or -1.
Example 1
Input
5 4 5 4 6 5
Expected
3
Explanation
4 and 5 each appear twice; 6 appears once. The first unique code is 6 at index 3.
Example 2
Input
3 7 7 7
Expected
-1
Explanation
Every code repeats, so there is no unique code: the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →