FastPrepFirst Duplicate Value

First Duplicate Value

Microsoft logoMicrosoftEasyFULLTIMEONSITE INTERVIEW
Learn

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[]) → int

Examples

Example 1

values = [2,1,3,5,3,2]return = 3

The second occurrence of 3 appears before the second occurrence of 2.

Example 2

values = [1,2,3,4]return = -1

Every value is unique.

Constraints

  • 0 <= values.length <= 200000.
  • 0 <= values[i] <= 1000000000.

More Microsoft problems

See Microsoft hiring insights
public int firstDuplicate(int[] values) {
    // Write your code here.
}
values[2,1,3,5,3,2]
expected3
Checking account…