#C11686. Find the First Duplicate Number
Find the First Duplicate Number
Find the First Duplicate Number
You are given an array of integers. Your task is to find the first duplicate number for which the second occurrence has the minimal index. In other words, when scanning the list from left to right, return the number that appears again first. If no duplicate exists, output -1
.
For example, in the list [2, 1, 3, 5, 3, 2], the number 3 is the first duplicate because its second occurrence appears before the second occurrence of 2.
inputFormat
The input is read from standard input (stdin) and has the following format:
The first line contains a single integer n, the number of elements in the array. The second line contains n space-separated integers representing the array elements.
outputFormat
Output a single integer to standard output (stdout): the first duplicate number as defined above. If there is no duplicate, output -1.## sample
6
2 1 3 5 3 2
3