How to useBit Manipulation (XOR) in Javascript

This approach uses the XOR operation to find the missing number efficiently. The XOR operation has the property that it cancels out duplicate numbers, which we can leverage to find the missing number.

Steps:

  • Initialize two variables, xorFull and xorArray.
  • XOR all numbers from 1 to 100 and store the result in xorFull.
  • XOR all the numbers in the given array and store the result in xorArray.
  • XOR the results of xorFull and xorArray. The result will be the missing number.

Example: In this example, we will remove a random number from the array and use the bit manipulation approach to find the missing number.

JavaScript
function findMissingNumber(arr) {
    const n = 100;
    let xorFull = 0;
    let xorArray = 0;

    for (let i = 1; i <= n; i++) {
        xorFull ^= i;
    }

    for (const num of arr) {
        xorArray ^= num;
    }

    const missingNumber = xorFull ^ xorArray;
    return missingNumber;
}

// Example array with 99 elements, removing the number 55
const array = Array.from({ length: 54 }, (_, index) => index + 1)
    .concat(Array.from({ length: 45 }, (_, index) => index + 56));
const missingNumber = findMissingNumber(array);
console.log("Missing number is:", missingNumber);

Output
Missing number is: 55




JavaScript Program to Find the Missing Number in a Given Integer Array of 1 to 100

In this article, we are going to find the missing number in a given integer array of 1 to 100 in JavaScript, Given an array [] of size 100 with integers in the range of [1, 100], There are no duplicate values in the array and no larger value than the size of the array. The task is to print that which is missing value in the array.

Let’s take an example:

Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, /* Missing number */,12, /* ... */, 100];  Size of an array is 100
Output: 11
Explanation: The missing number between 1 to 100 is 11

There are several methods that can be used to find the missing number in a given integer array of 1 to 100 in JavaScript, which are listed below:

Table of Content

  • Approach 1: Using the Mathematical Formulae
  • Approach 2: Using the array iteration
  • Approach 3: Using Object
  • Approach 4: Using Bit Manipulation (XOR)

We will explore all the above methods along with their basic implementation with the help of examples.

Similar Reads

Approach 1: Using the Mathematical Formulae

Expected Sum = (n * (n + 1)) / 2...

Approach 2: Using the array iteration

This approach involves iterating through the array and checking for the missing number. It is suitable when there are multiple missing numbers, but it is less efficient compared to the mathematical formula for finding a single missing number....

Approach 3: Using Object

Using an Object to track number’s presence, iterate through 1 to 100, and return the first missing number from the given array....

Approach 4: Using Bit Manipulation (XOR)

This approach uses the XOR operation to find the missing number efficiently. The XOR operation has the property that it cancels out duplicate numbers, which we can leverage to find the missing number....