Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding one missing number in a scrambled sequence using JavaScript
We are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n. The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array.
Problem Statement
Given an array of numbers from 1 to n with one missing number, find the missing number. The array is not sorted and contains n-1 elements instead of n.
Using Sum Formula Method
The most efficient approach uses the mathematical formula for the sum of first n natural numbers: sum = n × (n + 1) / 2. We calculate the expected sum and subtract the actual sum to find the missing number.
const arr = [4, 7, 1, 8, 9, 5, 2, 3];
const findMissing = (arr = []) => {
const sumArr = arr.reduce((acc, val) => acc + val);
const { length: len } = arr;
const sumFirst = (len + 1) * (len + 2) * 0.5;
const missing = sumFirst - sumArr;
return missing;
};
console.log("Array:", arr);
console.log("Missing number:", findMissing(arr));
Array: [4, 7, 1, 8, 9, 5, 2, 3] Missing number: 6
Using Set Lookup Method
An alternative approach uses a Set to check which number from 1 to n+1 is missing from the array.
const findMissingWithSet = (arr = []) => {
const numSet = new Set(arr);
const n = arr.length + 1;
for (let i = 1; i <= n; i++) {
if (!numSet.has(i)) {
return i;
}
}
};
const testArray = [1, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("Array:", testArray);
console.log("Missing number:", findMissingWithSet(testArray));
Array: [1, 3, 4, 5, 6, 7, 8, 9, 10] Missing number: 2
How the Sum Formula Works
For an array with n-1 elements (missing one number from 1 to n):
- Expected sum:
n × (n + 1) / 2 - Actual sum: Sum of all elements in the array
- Missing number: Expected sum - Actual sum
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Sum Formula | O(n) | O(1) | Most efficient |
| Set Lookup | O(n) | O(n) | Easy to understand |
Complete Example
function findMissingNumber(arr) {
// Method 1: Sum formula (recommended)
const actualSum = arr.reduce((sum, num) => sum + num, 0);
const n = arr.length + 1; // Total numbers should be n
const expectedSum = (n * (n + 1)) / 2;
return expectedSum - actualSum;
}
// Test with different arrays
const tests = [
[1, 2, 4, 5, 6], // Missing: 3
[2, 3, 4, 5, 6, 7], // Missing: 1
[1, 2, 3, 4, 6, 7] // Missing: 5
];
tests.forEach((test, index) => {
console.log(`Test ${index + 1}: [${test.join(', ')}] ? Missing: ${findMissingNumber(test)}`);
});
Test 1: [1, 2, 4, 5, 6] ? Missing: 3 Test 2: [2, 3, 4, 5, 6, 7] ? Missing: 1 Test 3: [1, 2, 3, 4, 6, 7] ? Missing: 5
Conclusion
The sum formula method is the most efficient solution with O(1) space complexity. It works by calculating the difference between the expected sum of consecutive numbers and the actual sum of the array.
