Solution 1: Math Formula (Simplest)
function findMissing(arr, n) {
// Expected sum of 1 to n
const expectedSum = n * (n + 1) / 2;
// Actual sum of array
const actualSum = arr.reduce((sum, num) => sum + num, 0);
return expectedSum - actualSum;
}
// Test
console.log(findMissing([1, 2, 4, 5], 5)); // 3
console.log(findMissing([1, 2, 3, 4], 5)); // 5
console.log(findMissing([2, 3, 4, 5], 5)); // 1
For Finding Duplicate
function findDuplicate(arr) {
const n = arr.length - 1; // Array has n+1 elements with one duplicate
const expectedSum = n * (n + 1) / 2;
const actualSum = arr.reduce((sum, num) => sum + num, 0);
return actualSum - expectedSum; // Duplicate number
}
console.log(findDuplicate([1, 2, 3, 4, 4])); // 4
console.log(findDuplicate([1, 1, 2, 3, 4])); // 1
Solution 2: XOR Bit Manipulation
function findMissingXOR(arr, n) {
let xor = 0;
// XOR all numbers from 1 to n
for (let i = 1; i <= n; i++) {
xor ^= i;
}
// XOR with all array elements
for (let num of arr) {
xor ^= num;
}
// Result is the missing number
// (all others cancel out because a ^ a = 0)
return xor;
}
// More concise version
function findMissingXORConcise(arr, n) {
let xor = 0;
for (let i = 1; i <= n; i++) xor ^= i;
for (let num of arr) xor ^= num;
return xor;
}
console.log(findMissingXOR([1, 2, 4, 5], 5)); // 3
Why XOR Works
Example: Find missing in [1, 2, 4, 5], n=5
XOR 1 to 5: 1 ^ 2 ^ 3 ^ 4 ^ 5
XOR array: 1 ^ 2 ^ 4 ^ 5
Combined: 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 2 ^ 4 ^ 5
= (1^1) ^ (2^2) ^ 3 ^ (4^4) ^ (5^5)
= 0 ^ 0 ^ 3 ^ 0 ^ 0
= 3
All paired numbers cancel (a ^ a = 0), leaving only the missing number.
Solution 3: Hash Set
function findMissingSet(arr, n) {
const seen = new Set(arr);
for (let i = 1; i <= n; i++) {
if (!seen.has(i)) {
return i;
}
}
return -1; // Not found (shouldn't happen with valid input)
}
console.log(findMissingSet([1, 2, 4, 5], 5)); // 3
Solution 4: In-Place Marking (Most Complex)
function findMissingInPlace(arr) {
const n = arr.length + 1;
// Mark indices by making them negative
for (let i = 0; i < arr.length; i++) {
const index = Math.abs(arr[i]) - 1;
if (index < arr.length) {
arr[index] = -Math.abs(arr[index]);
}
}
// Find the positive index (or unmapped)
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
return i + 1;
}
}
return n; // Last number is missing
}
// Note: This modifies the array!
let test = [1, 2, 4, 5];
console.log(findMissingInPlace(test)); // 3
console.log(test); // Array is now modified
Comparison
| Method |
Time |
Space |
Pros |
Cons |
| Math |
O(n) |
O(1) |
Cleanest |
Overflow risk for large n |
| XOR |
O(n) |
O(1) |
No overflow, shows bit skills |
Less intuitive |
| Hash Set |
O(n) |
O(n) |
Easy to understand |
Extra space |
| In-place |
O(n) |
O(1) |
Space efficient |
Modifies array |
Edge Cases
- n = 1: Array is empty, missing is 1
- Missing last number: All methods handle this
- Large n: Math approach might overflow, XOR is safer
- Array not sorted: All methods work regardless
Interview recommendation: Start with math approach (clean and intuitive). If asked for another method, use XOR (shows bit manipulation knowledge).
Related Problems