0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Missing Number in Array

You can find the missing number in an array of numbers from 1 to n by calculating the expected sum with n * (n + 1) / 2 and subtracting the actual sum of the array using const missing = totalSum - actualSum;.
📋

Examples

Input[1, 2, 4, 5, 6]
Output3
Input[2, 3, 1, 5]
Output4
Input[1]
Output2
🧠

How to Think About It

To find the missing number, first understand that the array should contain all numbers from 1 to n except one missing number. Calculate the sum of all numbers from 1 to n using the formula n * (n + 1) / 2. Then, add all numbers in the given array. The difference between these two sums is the missing number.
📐

Algorithm

1
Get the length of the array and add 1 to find n (the total count including the missing number).
2
Calculate the expected total sum of numbers from 1 to n using the formula n * (n + 1) / 2.
3
Calculate the actual sum of all numbers in the array.
4
Subtract the actual sum from the expected total sum to find the missing number.
5
Return the missing number.
💻

Code

javascript
function findMissingNumber(arr) {
  const n = arr.length + 1;
  const totalSum = (n * (n + 1)) / 2;
  const actualSum = arr.reduce((sum, num) => sum + num, 0);
  return totalSum - actualSum;
}

const array = [1, 2, 4, 5, 6];
console.log(findMissingNumber(array));
Output
3
🔍

Dry Run

Let's trace the array [1, 2, 4, 5, 6] through the code to find the missing number.

1

Calculate n

Array length is 5, so n = 5 + 1 = 6.

2

Calculate totalSum

totalSum = 6 * (6 + 1) / 2 = 6 * 7 / 2 = 21.

3

Calculate actualSum

Sum of array elements = 1 + 2 + 4 + 5 + 6 = 18.

4

Find missing number

missing = totalSum - actualSum = 21 - 18 = 3.

StepValue
n6
totalSum21
actualSum18
missing3
💡

Why This Works

Step 1: Calculate expected sum

Using the formula n * (n + 1) / 2 gives the sum of all numbers from 1 to n, which is what the array should have if no number was missing.

Step 2: Sum actual array values

Adding all numbers in the array shows what is actually present.

Step 3: Subtract to find missing

The difference between expected and actual sums reveals the missing number because that value was not added in the actual sum.

🔄

Alternative Approaches

Using a Set to find missing number
javascript
function findMissingNumberSet(arr) {
  const n = arr.length + 1;
  const set = new Set(arr);
  for (let i = 1; i <= n; i++) {
    if (!set.has(i)) return i;
  }
}

console.log(findMissingNumberSet([1, 2, 4, 5, 6]));
This method uses extra memory for the set but is easy to understand and works well for unsorted arrays.
Sorting and checking sequence
javascript
function findMissingNumberSort(arr) {
  arr.sort((a, b) => a - b);
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] !== i + 1) return i + 1;
  }
  return arr.length + 1;
}

console.log(findMissingNumberSort([1, 2, 4, 5, 6]));
Sorting changes the array and takes more time but is intuitive by checking where the sequence breaks.

Complexity: O(n) time, O(1) space

Time Complexity

Calculating the sum and iterating once over the array takes linear time, O(n).

Space Complexity

Only a few variables are used, so space complexity is constant, O(1).

Which Approach is Fastest?

The sum formula approach is fastest and uses least memory compared to set or sorting methods.

ApproachTimeSpaceBest For
Sum formulaO(n)O(1)Fastest and memory efficient
Set methodO(n)O(n)Easy to understand, works with unsorted arrays
Sorting methodO(n log n)O(1)When array needs sorting anyway
💡
Use the sum formula to find the missing number quickly without extra space.
⚠️
Forgetting to add 1 to the array length to get the total count including the missing number.