Problem · Array

Find Missing and Duplicate Integer

Learn this problem
MediumInMobi logoInMobiFULLTIMEPHONE SCREEN

Problem statement

You are given an integer array nums of length n. It should contain every integer from 1 through n exactly once, but one value is missing and one other value appears twice.

Return a two-element array [missing, duplicate], with the missing value first and the repeated value second.

Function

findMissingAndDuplicate(nums: int[]) → int[]

Examples

Example 1

nums = [1,2,3,4,5,6,7,8,9,10,10]return = [11,10]

The array has length 11. The value 11 is absent, while 10 appears twice.

Example 2

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

The value 4 is missing from 1 through 5, and 3 is duplicated.

Constraints

  • 2 <= nums.length <= 2 * 10^5
  • 1 <= nums[i] <= nums.length
  • Exactly one value in 1..nums.length is missing.
  • Exactly one different value appears twice, and every other value appears once.

More InMobi problems

drafts saved locally
public int[] findMissingAndDuplicate(int[] nums) {
    // write your code here
}
nums[1,2,3,4,5,6,7,8,9,10,10]
expected[11,10]
checking account