Find XOR of all elements in an Array - GeeksforGeeks

22 Mar, 2022
Given an array arr[] containing integers of size N, the task is to find the XOR of this array.Examples:  
Input: arr[] = {2, 4, 7} Output: 1 Explanation: XOR of the array = 2 ^ 4 ^ 7 = 1Input: arr[] = { 3, 9, 12, 13, 15 } Output: 4  
 
Approach: In order to find the XOR of all elements in the array, we simply iterate through the array and find the XOR using ‘^’ operator. Therefore, the following steps are followed to compute the answer:  
Create a variable to store the XOR of the array as a result.For each element in the array, find the XOR of the element and the result variable using ‘^’ operator.Finally, the result variable stores the XOR of all elements in the array.
Create a variable to store the XOR of the array as a result.
For each element in the array, find the XOR of the element and the result variable using ‘^’ operator.
Finally, the result variable stores the XOR of all elements in the array.
Below is the implementation of the above approach:  
CPP
C
Java
Python3
C#
Javascript
// C++ program to find the XOR of// all elements in the array #include <bits/stdc++.h>using namespace std; // Function to find the XOR of// all elements in the arrayint xorOfArray(int arr[], int n){    // Resultant variable    int xor_arr = 0;     // Iterating through every element in    // the array    for (int i = 0; i < n; i++) {         // Find XOR with the result        xor_arr = xor_arr ^ arr[i];    }     // Return the XOR    return xor_arr;} // Driver Codeint main(){     int arr[] = { 3, 9, 12, 13, 15 };    int n = sizeof(arr) / sizeof(arr[0]);     // Function call    cout << xorOfArray(arr, n) << endl;     return 0;}
// C program to find the XOR of// all elements in the array#include <stdio.h> // Function to find the XOR of// all elements in the arrayint xorOfArray(int arr[], int n){    // Resultant variable    int xor_arr = 0;     // Iterating through every element in    // the array    for (int i = 0; i < n; i++) {         // Find XOR with the result        xor_arr = xor_arr ^ arr[i];    }     // Return the XOR    return xor_arr;} // Driver Codeint main(){     int arr[] = { 3, 9, 12, 13, 15 };    int n = sizeof(arr) / sizeof(arr[0]);     // Function Call    printf("%d\n", xorOfArray(arr, n));    return 0;} // This code is contributed by phalashi.
// Java program to find the XOR of// all elements in the arrayclass GFG {         // Function to find the XOR of    // all elements in the array    static int xorOfArray(int arr[], int n)    {        // Resultant variable        int xor_arr = 0;             // Iterating through every element in        // the array        for (int i = 0; i < n; i++) {                 // Find XOR with the result            xor_arr = xor_arr ^ arr[i];        }             // Return the XOR        return xor_arr;    }         // Driver Code    public static void main (String[] args)    {             int arr[] = { 3, 9, 12, 13, 15 };        int n = arr.length;             // Function call        System.out.println(xorOfArray(arr, n));     }} // This code is contributed by Yash_R
# Python3 program to find the XOR of# all elements in the array # Function to find the XOR of# all elements in the arraydef xorOfArray(arr, n):     # Resultant variable    xor_arr = 0     # Iterating through every element in    # the array    for i in range(n):         # Find XOR with the result        xor_arr = xor_arr ^ arr[i]     # Return the XOR    return xor_arr # Driver Codeif __name__ == '__main__':    arr = [3, 9, 12, 13, 15]    n = len(arr)     # Function call    print(xorOfArray(arr, n)) # This code is contributed by mohit kumar 29
// C# program to find the XOR of// all elements in the arrayusing System; class GFG {         // Function to find the XOR of    // all elements in the array    static int xorOfArray(int []arr, int n)    {        // Resultant variable        int xor_arr = 0;             // Iterating through every element in        // the array        for (int i = 0; i < n; i++) {                 // Find XOR with the result            xor_arr = xor_arr ^ arr[i];        }             // Return the XOR        return xor_arr;    }         // Driver Code    public static void Main (string[] args)    {             int []arr = { 3, 9, 12, 13, 15 };        int n = arr.Length;             // Function call        Console.WriteLine(xorOfArray(arr, n));    }} // This code is contributed by AnkitRai01
<script> // JavaScript program to find the XOR of// all elements in the array // Function to find the XOR of// all elements in the arrayfunction xorOfArray(arr, n){    // Resultant variable    let xor_arr = 0;     // Iterating through every element in    // the array    for (let i = 0; i < n; i++) {         // Find XOR with the result        xor_arr = xor_arr ^ arr[i];    }     // Return the XOR    return xor_arr;} // Driver Code    let arr = [ 3, 9, 12, 13, 15 ];    let n = arr.length;     // Function call    document.write(xorOfArray(arr, n) + "<br>"); // This code is contributed by Surbhi Tyagi. </script>
4
 
Time Complexity: O(N), where N is the size of the array. 
mohit kumar 29
Yash_R
ankthon
surbhityagi15
phasing17
Bitwise-XOR
number-digits
Arrays
Bit Magic
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?