Find The Missing Number From The Array — JavaScript

Umar Farooque Khan
3 min readDec 2, 2023

--

Discovering the absent number within an array presents multiple strategies in JavaScript. Below, I outline a couple of distinctive approaches for this task.

Approach 1:

Using Arithmetic Progression Formula

function findMissingNumber(arr) {
const n = arr.length + 1;
const expectedSum = (n * (n + 1)) / 2;
const actualSum = arr.reduce((sum, num) => sum + num, 0);
return expectedSum - actualSum;
}
const array = [1, 2, 4, 6, 3, 7, 8];
const uniqueArray = [...new Set(array)];
const missingNumber = findMissingNumber(uniqueArray);
console.log(`Missing number: ${missingNumber}`);

Output:

Explanation:

The findMissingNumber function efficiently identifies the missing element in an array. By leveraging the arithmetic sum formula, it calculates the expected sum for a complete sequence of numbers up to the array's length. The actual sum of the array is then computed using the reduce method. The missing number is obtained by subtracting the actual sum from the expected sum. In the provided example with array1, the missing number is determined and displayed using console.log. This concise and effective approach simplifies the process of identifying missing numbers in arrays.

Approach 2:

Using Summation Formula

function findMissingNumber(inputArray, length) {
let result = Math.floor((length + 1) * (length + 2) / 2);
for (let i = 0; i < length; i++) result -= inputArray[i];
return result;
}

let myArray = [1, 2, 3, 5];
let missingNumber = findMissingNumber(myArray, myArray.length);
document.write(missingNumber);

Output

Explanation:

The approach employed in the given JavaScript code utilizes the Gauss formula, also known as the arithmetic sum formula. This formula calculates the expected sum of a sequence of consecutive numbers up to a certain value. By subtracting the actual sum of the array elements from this expected sum, the code efficiently identifies and returns the missing number in the array.

Approach 3:

Using XOR

function findMissingNumberWithXOR(sequence) {
const length = sequence.length + 1; // Including the missing number
let xorOfAllNumbers = 0;

for (let i = 1; i <= length; i++) {
xorOfAllNumbers ^= i;
}

let xorOfArrayElements = sequence.reduce((acc, num) => acc ^ num, 0);

return xorOfAllNumbers ^ xorOfArrayElements;
}

const inputSequence = [2, 4, 6, 8, 10, 12, 14];
const result = findMissingNumberWithXOR(inputSequence);
console.log(`Missing number in the sequence ${inputSequence}: ${result}`);

Output:

Explanation:

The modified JavaScript code utilizes XOR operations to find the missing number in an array. The function, now named findMissingNumberWithXOR, XORs the expected and actual XOR values of the sequence and array elements, respectively. The result effectively identifies the missing number. The example usage with myArray demonstrates this XOR-based approach, highlighting the missing number in the array.

Here are some additional high-quality tutorials for you to explore:

  1. Unraveling JavaScript Design Patterns
  2. JavaScript interview Question and Answer
  3. Node Js Interview Question and Answer
  4. JavaScript Tricky Question
  5. Second largest element in an array

If you enjoyed this article, please give it a round of applause👏👏👏👏 and consider following me for support!

--

--

Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.