EasyArrayHash TableBit ManipulationSortingUpdated Sep 2026

645. Set Mismatch

Asked at Microsoft

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Array

Companies

1

Problem

You are given an array nums that should contain every integer from 1 to n exactly once, where n is the length of the array. But one value got copied over another value, so now one number appears twice and one number is missing.

Return a two-element array [duplicate, missing]. The first element is the number that shows up twice, and the second is the number that never shows up.

You may assume the array always has exactly one duplicate and exactly one missing value, so there is always a single correct answer.

One value copied, one value lost

nums122401232 appears at index 1 and index 2 — that is theduplicateexpected12340123The value 3 is missing from nums, so the answer is[2,3]
The array should be a permutation of 1..n, but one slot holds a copy of another value.

Constraints

  • ●2 <= nums.length <= 10^4
  • ●1 <= nums[i] <= 10^4

Examples

Example 1
Inputnums = [1,2,2,4]
Output[2,3]
WhyThe array should hold 1,2,3,4. The value 2 appears twice, and 3 is nowhere in the array, so the answer is [2,3].
Example 2
Inputnums = [1,1]
Output[1,2]
WhyWith n = 2 the array should hold 1 and 2. The 2 was replaced by a second 1, so 1 is the duplicate and 2 is the missing number.

Asked At

1 companies
CompanyDifficulty
MicrosoftEasyView all Microsoft questions →

How to Think About It

1

Brute force: sort the array and scan for the duplicate and missing numbers in a single pass.

2

Use a hash set to track seen numbers; the duplicate appears twice, and the missing number is absent.

3

Math approach: compute sum and sum of squares to derive the duplicate and missing via algebra.

4

Bit manipulation: XOR all elements with 1..n to isolate the duplicate and missing bits.

5

Optimal: use the sign-flipping technique by marking visited indices as negative to identify both numbers in one pass.

Optimal Approach

Compute the expected sum S = n(n+1)/2 and expected sum of squares Q = n(n+1)(2n+1)/6. The actual sum gives us (duplicate - missing) and the actual sum of squares gives (duplicate^2 - missing^2). Solving these two equations yields both values. Alternatively, iterate through the array and mark the index corresponding to each value by negating it; a repeated value means the index was already negated (duplicate), and the positive index at the end is the missing number.

XOR everything, then split by one bit

xor_all = (1^2^3^4) ^ (1^2^2^4)= 2 ^ 3 = 1xor_all = duplicate ^ missingpick lowest set bit of xor_allsplit 1..n and nums into two groups by that XOR each group → x and yThe set bit is where duplicate and missing differ, soit puts them in different groups.nums12240123x = 2 is found inside nums, so 2 is the duplicate andy = 3 is the missing number
XOR of 1..n and nums leaves only duplicate ^ missing, and any set bit separates the two values.
ComplexityTime O(n)Space O(1)We make a few linear passes over the array and use only a constant number of extra variables.

What Trips People Up in Real Interviews

1

Clarify the range is 1..n and exactly one duplicate and one missing exist.

2

Do not confuse this with finding duplicates only — the missing number is equally important.

3

The bit manipulation XOR approach is elegant but tricky to explain under time pressure.

4

The sign-flipping in-place method is O(1) space but modifies the input array.

5

Confirm whether returning the pair [duplicate, missing] or separate variables.

Solution Code

O(n) time
def findErrorNums(nums):
    n = len(nums)
    xor_all = 0
    for i in range(1, n + 1):
        xor_all ^= i
    for num in nums:
        xor_all ^= num
    diff_bit = xor_all & (-xor_all)
    x = 0
    y = 0
    for i in range(1, n + 1):
        if i & diff_bit:
            x ^= i
        else:
            y ^= i
    for num in nums:
        if num & diff_bit:
            x ^= num
        else:
            y ^= num
    for num in nums:
        if num == x:
            return [x, y]
    return [y, x]

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Set Mismatch problem?

You are given a set of integers where one number is duplicated and one number is missing. Find both the duplicate and the missing number using O(n) time and O(1) space.

How do you solve Set Mismatch?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Set Mismatch?

Set Mismatch is asked at Microsoft. It is a easy difficulty problem.

What are common mistakes on Set Mismatch?
  • Clarify the range is 1..n and exactly one duplicate and one missing exist.
  • Do not confuse this with finding duplicates only — the missing number is equally important.
  • The bit manipulation XOR approach is elegant but tricky to explain under time pressure.
  • The sign-flipping in-place method is O(1) space but modifies the input array.
  • Confirm whether returning the pair [duplicate, missing] or separate variables.