FastPrepFind the Duplicate Number

Find the Duplicate Number

SpaceX logoSpaceX● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Find the Duplicate Number

You are given an integer array nums of length n. It contains every integer from 1 through n - 1 exactly once, except for one value that appears twice.

Return the duplicated value.

Function

findDuplicateNumber(nums: int[]) → int

Examples

Example 1

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

The value 2 is the only number that appears twice.

Example 2

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

The value 3 appears at two positions.

Example 3

nums = [1,1]return = 1

With n = 2, the only valid value is 1, and it appears twice.

Constraints

  • 2 ≤ nums.length ≤ 100,000
  • Every value is between 1 and nums.length - 1, inclusive.
  • Exactly one distinct value appears twice, and every other value appears once.

More SpaceX problems

See SpaceX hiring insights
public int findDuplicateNumber(int[] nums) {
    // Write your code here.
}
nums[1,3,4,2,2]
expected2
Checking account…