class Solution:
def findErrorNums(self, nums):
n = len(nums)
num_set = set()
duplicate = -1
for num in nums:
if num in num_set:
duplicate = num
else:
num_set.add(num)
for i in range(1, n + 1):
if i not in num_set:
missing = i
break
return [duplicate, missing]
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
int n = nums.size();
vector<bool> seen(n + 1, false);
int duplicate = -1, missing = -1;
for (int num : nums) {
if (seen[num]) {
duplicate = num;
} else {
seen[num] = true;
}
}
for (int i = 1; i <= n; ++i) {
if (!seen[i]) {
missing = i;
break;
}
}
return {duplicate, missing};
}
};
class Solution {
public int[] findErrorNums(int[] nums) {
int n = nums.length;
boolean[] seen = new boolean[n + 1];
int duplicate = -1, missing = -1;
for (int num : nums) {
if (seen[num]) {
duplicate = num;
} else {
seen[num] = true;
}
}
for (int i = 1; i <= n; i++) {
if (!seen[i]) {
missing = i;
break;
}
}
return new int[]{duplicate, missing};
}
}
var findErrorNums = function(nums) {
let n = nums.length;
let seen = new Array(n + 1).fill(false);
let duplicate = -1, missing = -1;
for (let num of nums) {
if (seen[num]) {
duplicate = num;
} else {
seen[num] = true;
}
}
for (let i = 1; i <= n; i++) {
if (!seen[i]) {
missing = i;
break;
}
}
return [duplicate, missing];
};
You are given an array nums
containing n
integers where the numbers are supposed to be the integers from 1
to n
(inclusive), but:
nums
appears twice (the duplicate).1
to n
is missing from nums
.
Your task is to find both the duplicate number and the missing number, and return them as a list or array: [duplicate, missing]
.
Constraints:
nums
is in the range [1, n]
.
The first idea that comes to mind is to count how many times each number from 1
to n
appears in nums
. If a number appears twice, it's the duplicate. If a number never appears, it's the missing one.
A brute-force way would be to check, for each number in 1
to n
, how many times it appears in nums
(using a loop inside a loop). But this would be slow for large arrays, as it would take O(n²) time.
To optimize, we can use a data structure that helps us count occurrences efficiently, like a hash map or an array. This way, we can find the duplicate and missing numbers in just two passes over the data.
The key insight is: by tracking which numbers we've seen, we can instantly spot the duplicate, and by checking which number is never seen, we find the missing one.
n+1
(since numbers go from 1 to n).nums
. For each number:
We use a set or boolean array because checking if a number has been seen is O(1) time, making the overall solution efficient. This approach is easy to understand and avoids unnecessary nested loops.
Let's walk through the example nums = [1, 2, 2, 4]
:
nums
:
[2, 3]
— 2 is the duplicate, 3 is missing.nums
(using a nested loop).
The optimized solution is much faster, especially as n
grows.
The Set Mismatch problem is solved efficiently by tracking which numbers have been seen using a set or boolean array. This lets us quickly identify the duplicate and missing numbers in a single scan, avoiding slow nested loops. The solution is elegant because it leverages simple data structures and clear logic, making it both fast and easy to understand.