Problem
LeetCode 645 · Easy. The array nums has length n and originally contained each integer from 1 through n exactly once. One number was replaced by another number already in the array. Return [duplicate, missing], in that order. Set Mismatch on LeetCode
Example 1
Input: nums = [1, 2, 2, 4]
Output: [2, 3]
Example 2
Input: nums = [1, 1]
Output: [1, 2]
2 ≤ nums.length ≤ 10⁴1 ≤ nums[i] ≤ 10⁴. The problem also guarantees that each value comes from the original range1…n.
My Solutions
Approach 1 · Track seen values with a Set
Scan nums and add each new value to a Set. If a value is already in the set, record it as the duplicate. Then check every candidate from 1 through n. The first candidate absent from the set is the missing number.
For [1, 2, 2, 4], the second 2 gives us the duplicate. The set ends up holding 1, 2, and 4, so the second pass finds 3 missing.
Animated walkthrough · Set

/**
* @param {number[]} nums
* @return {number[]}
*/
var findErrorNums = function (nums) {
let duplicate = -1;
let missing = -1;
const seen = new Set();
for (const num of nums) {
if (seen.has(num)) {
duplicate = num;
} else {
seen.add(num);
}
}
for (let value = 1; value <= nums.length; value++) {
if (!seen.has(value)) {
missing = value;
break;
}
}
return [duplicate, missing];
};
The first approach runs in O(n) time with the usual constant-time assumption for Set lookups and inserts. The set takes O(n) auxiliary space, and the input array is preserved.
Approach 2 · Mark seen values in place
The set records whether we have seen each value. Since every value v lies in 1…n, we can store that flag in the sign of nums[v - 1]. A positive sign means its corresponding value is unseen. A negative sign means it has already appeared.
- Read the current value with
Math.abs(nums[i]). Its slot may have been marked earlier, and the absolute value recovers the original number. - Look at the slot at index
v - 1. If it is already negative,vis the duplicate. Otherwise, flip the slot to negative. - Scan the marked array for its one positive slot. At index
i, that slot represents the missing valuei + 1.
For [1, 2, 2, 4], the first 2 flips nums[1] to -2, and the second 2 finds the sign already negative. The final array is [-1, -2, 2, -4]. The positive 2 sits at index 2, which represents the missing value 3.
Animated walkthrough · In-place sign marking

/**
* @param {number[]} nums
* @return {number[]}
*/
var findErrorNums = function (nums) {
let duplicate = -1;
let missing = -1;
for (let i = 0; i < nums.length; i++) {
const value = Math.abs(nums[i]);
const index = value - 1;
if (nums[index] < 0) {
duplicate = value;
} else {
nums[index] = -nums[index];
}
}
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
missing = i + 1;
break;
}
}
return [duplicate, missing];
};
The second approach runs in O(n) time and uses O(1) auxiliary space. This version changes the signs in nums. If the caller needs the original values back, a final pass with Math.abs restores the array in O(n) time and O(1) auxiliary space.
Trade-off
| Approach | Time | Auxiliary space | Input |
|---|---|---|---|
| Set | O(n) | O(n) | Preserved |
| In-place sign marking | O(n) | O(1) | Signs changed |
Takeaway
When optimizing memory, first look for a way to store the flag in data you already have. In Set Mismatch, v - 1 gives each value its own slot. The sign holds the seen flag, and the magnitude keeps the original value available through Math.abs. Reusing those slots removes the allocation for the set and cuts auxiliary space from O(n) to O(1). Both approaches still take O(n) time.