The Wayback Machine - https://web.archive.org/web/20210418155001/https://www.geeksforgeeks.org/how-to-calculate-the-xor-of-array-elements-using-javascript/
Skip to content
Related Articles

Related Articles

How to calculate the XOR of array elements using JavaScript ?
  • Last Updated : 15 Apr, 2020

Given an array and is the task to find the XOR of Array elements using JavaScript. There are two approaches to solve this problem which are discussed below:

Simple method: It uses a simple method to access the array elements by an index number and use the loop to find the XOR of values of an Array using JavaScript.

Example: This example uses a simple method to find the XOR of Array elements using JavaScript.




<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        How to Find the XOR of Values 
        of an Array in JavaScript? 
    </title> 
</head> 
  
<body style="text-align:center;"> 
      
    <h1 style = "color:green;" > 
        GeeksForGeeks 
    </h1> 
      
    <h3> 
        How to Find the XOR of Values 
        of an Array in JavaScript? 
    </h3> 
      
    <h4> 
        ----Given Array----<br> 
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
    </h4> 
      
    <button onclick="myGeeks()">Click</button> 
      
    <p id="gfg"></p> 
      
    <script> 
        function XOR(input)
        { 
            if (toString.call(input) !== "[object Array]") 
            return false; 
          
            var total = Number(input[0]); 
            for(var i=1;i<input.length;i++) {                 
                if(isNaN(input[i])){ 
                    continue; 
                } 
              
                total ^= Number(input[i]); 
            } 
            return total; 
        } 
        function myGeeks(item) { 
                document.getElementById("gfg").innerHTML
                    = "----XOR of Array----" + "<br>" 
                    + XOR([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 
            }
    </script> 
</body> 
  
</html>                    

Output:

  • Before click on the Button:
  • After click on the Button:

Using reduce() method: The array reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator.



Syntax:

array.reduce( function( total, currentValue, currentIndex, arr ), initialValue )

Example: This example uses array reduce() method to find the XOR of values of an Array using JavaScript.




<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        How to Find the XOR of Values 
        of an Array in JavaScript? 
    </title> 
</head> 
  
<body style="text-align:center;"> 
      
    <h1 style = "color:green;" > 
        GeeksForGeeks 
    </h1> 
      
    <h3> 
        How to Find the XOR of Values 
        of an Array in JavaScript? 
    </h3> 
      
    <h4> 
        ----Given Array----<br> 
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
    </h4> 
      
    <button onclick="myGeeks()"> 
        Click Here! 
    </button> 
      
    <br><br> 
      
    XOR: <span id="GFG"></span> 
      
    <script> 
        var arr=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 
          
        function ORofArray(XOR, num) { 
            return XOR ^ num; 
        } 
        function myGeeks(item) { 
            document.getElementById("GFG").innerHTML 
                = arr.reduce(ORofArray); 
        } 
    </script> 
</body> 
  
</html>                    

Output:

  • Before click on the Button:
  • After click on the Button:

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :