#C12624. Find the First Duplicate Number
Find the First Duplicate Number
Find the First Duplicate Number
Given a sequence of integers, find the first duplicate number in the sequence. In other words, as you traverse the sequence from left to right, identify the first number that has already appeared before. If none of the numbers are duplicated in the order of appearance, output \(-1\).
Note: The solution should read input from standard input (stdin) and output the result to standard output (stdout).
The input format is as follows:
- The first line contains an integer \(n\), representing the number of integers.
- The second line contains \(n\) integers separated by spaces.
The output is a single integer indicating the first duplicate number. If no duplicate exists, output \(-1\).
inputFormat
The input begins with an integer (n) on the first line, representing the number of integers. The second line contains (n) space-separated integers.
outputFormat
Output a single integer—the first duplicate occurrence as encountered from left to right. If no duplicate exists, output (-1).## sample
6
2 3 1 2 4 3
2