
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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

- Related Articles
- Find elements of array using XOR of consecutive elements in C++
- How to calculate sum of array elements using pointers in C language?
- How to unpack array elements into separate variables using JavaScript?
- How to replace elements in array with elements of another array in JavaScript?
- Find the OR and AND of Array elements using JavaScript
- Minimizing array sum by applying XOR operation on all elements of the array in C++
- Remove elements from array using JavaScript filter - JavaScript
- Deep count of elements of an array using JavaScript
- XOR of all elements of array with set bits equal to K in C++
- How to duplicate elements of an array in the same array with JavaScript?
- Minimum number of elements to be removed to make XOR maximum using C++.
- How to add new array elements at the beginning of an array in JavaScript?
- How to move multiple elements to the beginning of the array in JavaScript?
- How to filter an array from all elements of another array – JavaScript?
- Frequency of elements of one array that appear in another array using JavaScript
