Set Mismatch
Give value v the home nums[v − 1] and swap every value home. Every value that exists reaches its home, except the second copy of the duplicate — its home is already taken. It gets left in the only free slot, which is the home of the missing value. So the one slot i with nums[i] != i + 1 holds the duplicate, and i + 1 is the missing value.
Keep swapping while nums[i] != nums[nums[i] − 1] (the home of nums[i] does not already hold it). Afterwards, the only i with nums[i] != i + 1 gives duplicate = nums[i] and missing = i + 1.
Problem
An array nums of length n was supposed to hold every number from 1 to n exactly once. By mistake, one number got copied over another one. So now one value appears twice and one value is missing.
Return both of them as [duplicate, missing] — the repeated value first, then the missing value.
An integer array nums of length n, with values in 1..n.
An array of two numbers: [duplicate, missing].
2 shows up twice and 3 never shows up.
1 is repeated, so 2 is missing.
- • 2 ≤ nums.length ≤ 10^4
- • 1 ≤ nums[i] ≤ 10^4
- • Exactly one value is repeated and exactly one value in 1..n is missing.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
The values should be exactly 1..n. How many of them can I check directly, one by one?
- 2.
If I could use extra memory, what would tell me "how many times does k appear?" quickly?
- 3.
The array has n slots and the values are 1..n. Is there a natural slot for each value?
- 4.
If every value moves into its own slot, what would the slot of the missing value end up holding?
- 5.
The answer wants two numbers. Which one comes first?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | For each k in 1..n, scan the array and count how often k appears | O(N²) | O(1) |
| Better | Count every value once in an array of size n + 1 | O(N) | O(N) |
| Optimal | Swap each value v into index v − 1; the one wrong slot gives both answers | O(N) | O(1) |
Why does this belong to this pattern?
- “numbers 1 to n”Each value v has a natural home: index v − 1.
- “one number repeated”One value finds its home already occupied by its twin.
- “one number missing”One home never receives its owner.
- “return both”The same wrong slot shows both: the value in it is the duplicate, its index + 1 is the missing value.
In the missing sub-pattern we only read the index of the wrong slot. In the duplicate sub-pattern we only read the value in it. Here the two meet: one value appears twice and one is missing, so the single wrong slot gives both — its value is the duplicate and its index + 1 is the missing number.
Don't make this mistake
This is the pattern-confusion trap. In the missing sub-pattern the index (i + 1) is the answer, so it is easy to put it first. Here the problem wants the duplicate first. On [1, 2, 2, 4] the wrong order returns [3, 2] instead of [2, 3].
This condition works when every value is distinct, but here one value is doubled. On [1, 1], at i = 1 the value 1 is not at index 1, so it is swapped with index 0 — which also holds 1. Nothing changes and the loop never ends. Asking "does the home already hold this value?" stops the duplicate.
After one swap, the value that lands at i may also need to move. With if, it can stay out of place. On [4, 3, 2, 7, 8, 2, 6, 1] the if version ends as [7, 2, 3, 4, 1, 6, 2, 8] with three wrong slots, and returns [7, 1] instead of [2, 5].
The missing value has nothing to do with the duplicate's value. In [3, 2, 3, 4, 6, 5] the duplicate is 3 and the missing value is 1.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Cyclic Sort translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.