645. Set Mismatch
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem presents us with an array nums of size n. This array was supposed to contain all integers from 1 to n exactly once. However, an error occurred: one number was duplicated, and consequently, one number from the 1 to n range went missing. Our task is to identify both the duplicated number and the missing number.
Solution
Set Mismatch in Python
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
dup, missing = -1, -1
# First pass: Use the array itself as a hash map.
# The sign of the number at an index indicates if the number corresponding to that index has been seen.
for num in nums:
# Get the index this number corresponds to.
# Numbers are 1-based, indices are 0-based. Use abs() because num could have been negated in a previous step.
index = abs(num) - 1
# If the number at this index is already negative, it means we've seen this number before.
# So, abs(num) is the duplicate.
if nums[index] < 0:
dup = abs(num)
else:
# Mark this number as seen by negating the value at its corresponding index.
nums[index] *= -1
# Second pass: Find the missing number.
# The index of the only positive number corresponds to the missing number.
for i in range(len(nums)):
if nums[i] > 0:
# The number i+1 is missing because nums[i] was never marked (negated).
missing = i + 1
break # We found the missing number, no need to continue.
return [dup, missing]
Set Mismatch in Java
class Solution {
public int[] findErrorNums(int[] nums) {
int dup = -1, missing = -1;
// First pass: Use the array itself as a hash map.
// The sign of the number at an index indicates if the number corresponding to that index has been seen.
for (int n : nums) {
// Get the index this number corresponds to.
// Numbers are 1-based, indices are 0-based. Use Math.abs because n could have been negated.
int index = Math.abs(n) - 1;
// If the number at this index is already negative, it means we've seen this number before.
// So, Math.abs(n) is the duplicate.
if (nums[index] < 0) {
dup = Math.abs(n);
} else {
// Mark this number as seen by negating the value at its corresponding index.
nums[index] = -nums[index];
}
}
// Second pass: Find the missing number.
// The index of the only positive number corresponds to the missing number.
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
// The number i+1 is missing because nums[i] was never marked (negated).
missing = i + 1;
break; // We found the missing number, no need to continue.
}
}
return new int[]{dup, missing};
}
}
Set Mismatch in C++
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
int dup = -1, missing = -1;
// First pass: Use the array itself as a hash map.
// The sign of the number at an index indicates if the number corresponding to that index has been seen.
for (int n : nums) {
// Get the index this number corresponds to.
// Numbers are 1-based, indices are 0-based.
int index = abs(n) - 1;
// If the number at this index is already negative, it means we've seen this number before.
// So, abs(n) is the duplicate.
if (nums[index] < 0) {
dup = abs(n);
} else {
// Mark this number as seen by negating the value at its corresponding index.
nums[index] = -nums[index];
}
}
// Second pass: Find the missing number.
// The index of the only positive number corresponds to the missing number.
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] > 0) {
// The number i+1 is missing because nums[i] was never marked (negated).
missing = i + 1;
break; // We found the missing number, no need to continue.
}
}
return {dup, missing};
}
};
Set Mismatch in TypeScript
function findErrorNums(nums: number[]): number[] {
let dup = -1, missing = -1;
// First pass: Use the array itself as a hash map.
// The sign of the number at an index indicates if the number corresponding to that index has been seen.
for (const num of nums) {
// Get the index this number corresponds to.
// Numbers are 1-based, indices are 0-based. Use Math.abs because num could have been negated.
const index = Math.abs(num) - 1;
// If the number at this index is already negative, it means we've seen this number before.
// So, Math.abs(num) is the duplicate.
if (nums[index] < 0) {
dup = Math.abs(num);
} else {
// Mark this number as seen by negating the value at its corresponding index.
nums[index] *= -1;
}
}
// Second pass: Find the missing number.
// The index of the only positive number corresponds to the missing number.
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
// The number i+1 is missing because nums[i] was never marked (negated).
missing = i + 1;
break; // We found the missing number, no need to continue.
}
}
return [dup, missing];
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.