FastPrepFind the Duplicate Number Without Modifying the Array

Find the Duplicate Number Without Modifying the Array

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

An array of length n+1 contains values from 1 through n. Exactly one value occurs more than once.

Return that duplicate without modifying the array and using constant extra space.

Function

findDuplicate(nums: int[]) → int

Examples

Example 1

nums = [1,3,4,2,2]return = 2

Two is the repeated value.

Example 2

nums = [3,1,3,4,2]return = 3

Three is repeated.

Example 3

nums = [1,1]return = 1

The smallest valid array repeats one.

Constraints

  • 1 <= n <= 100000.
  • Exactly one distinct value is duplicated, possibly more than twice.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int findDuplicate(int[] nums) {
    // write your code here
}
nums[1,3,4,2,2]
expected2
Checking account…