First Duplicate Value
Problem statement
Scan values from left to right. Return the first value whose current occurrence has appeared earlier in the array. Equivalently, choose the duplicate whose second occurrence has the smallest index.
Return -1 when no value occurs twice.
Function
firstDuplicate(values: int[]) → intExamples
Example 1
values = [2,1,3,5,3,2]return = 3The second occurrence of 3 appears before the second occurrence of 2.
Example 2
values = [1,2,3,4]return = -1Every value is unique.
Constraints
0 <= values.length <= 200000.0 <= values[i] <= 1000000000.