All problems

First Repeat Visitor

Easy · hash map

A shop logs the id of every visitor who walks in, in the order they arrive. The list visits holds those ids, so the same id appears more than once if that person came back.

Find the first moment someone is seen again: look for the earliest position in visits whose id has already appeared at some earlier position. Return that id.

If no id appears twice, return -1.

Examples

Example 1

Input: visits = [4, 7, 2, 7, 4]

Output: 7

The second 7 is at position 3 and the second 4 is at position 4. Position 3 comes first, so the answer is 7.

Example 2

Input: visits = [1, 2, 3]

Output: -1

Nobody comes back.

Example 3

Input: visits = [5, 5, 5]

Output: 5

Constraints