How to calculate the XOR of array elements using JavaScript?


We will use a for loop to iterate through the array. We will initialize a variable called "result" with the value of the first element in the array. For each subsequent element in the array, we will use the XOR operator to update the value of "result" with that element. This process will continue until all elements in the array have been processed, resulting in the final XOR value of all elements in the array.

Let us first understand what XOR is. We will also see how XOR operation on an array works.

Array XOR

  • The XOR (exclusive or) operation is a bitwise operation that compares two binary digits and returns 1 if they are different and 0 if they are the same.

  • An XOR operation on an array is a way to combine the values of all elements in the array using the XOR operation.

  • In an array A of size n, the XOR of all elements can be calculated by using the following formula: A[0]A[1]A[2]…A[n-1].

  • XORing all element of array gives the number that occurs only once in array.

  • An important application of XOR operation on arrays is in finding the single number in an array that occurs only once while all other elements occur twice.

Approach

Here is one way you can calculate the XOR of an array of elements using JavaScript −

function calculateXOR(arr) {
   let result = arr[0];
   for (let i = 1; i < arr.length; i++) {
      result = result ^ arr[i];
   }
   return result;
}

You can call this function by passing in an array of elements as the argument, like this −

let arr = [1, 2, 3, 4, 5];
let xor = calculateXOR(arr);
console.log(xor);

This will output the XOR of all elements in the array.

Note − XOR operation is bitwise operation in JavaScript, it compares each bit of the first operand to the corresponding operand in the second operand. If both bits are the same then the corresponding bit in the result is 0, otherwise it is 1.

Example

Here is an example of how to calculate the XOR of all elements in an array using JavaScript −

function xorArray(arr) {
   let result = arr[0];
   for (let i = 1; i < arr.length; i++) {
      result = result ^ arr[i];
   }
   return result;
}
let testArray = [1, 2, 3, 4, 5];
console.log(xorArray(testArray)); 

Explanation

  • The function xorArray(arr) takes in an array as a parameter.

  • The variable result is initialized to the first element of the array.

  • A for loop is used to iterate through the array starting from the second element.

  • On each iteration, the result variable is updated using the XOR (^) operator. The XOR operator compares each bit of the first operand to the corresponding bit of the second operand. If the bits are the same, the corresponding result bit is 0. If the bits are different, the corresponding result bit is 1.

  • The final value of the result after the for loop is the XOR of all elements in the array.

  • The function returns the final result.

  • A test array is defined and the function is called with the test array as an argument. The output of the function is logged to the console.

Output

Updated on: 16-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements