Related Articles
Find XOR of all elements in an Array
• Difficulty Level : Easy
• Last Updated : 21 Apr, 2020

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 = 1

Input: arr[] = { 3, 9, 12, 13, 15 }
Output: 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. Create a variable to store the XOR of the array as a result.
2. For each element in the array, find the XOR of the element and the result variable using ‘^’ operator.
3. Finally, the result variable stores the XOR of all elements in the array.

Below is the implementation of the above approach:

## CPP

 `// C++ program to find the XOR of ` `// all elements in the array ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the XOR of ` `// all elements in the array ` `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 ` `int` `main() ` `{ ` ` `  `    ``int` `arr[] = { 3, 9, 12, 13, 15 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Function call ` `    ``cout << xorOfArray(arr, n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find the XOR of ` `// all elements in the array ` `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 ` `        ``System.out.println(xorOfArray(arr, n)); ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by Yash_R `

## Python3

 `# Python3 program to find the XOR of ` `# all elements in the array ` ` `  `# Function to find the XOR of ` `# all elements in the array ` `def` `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 Code ` `if` `__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#

 `// C# program to find the XOR of ` `// all elements in the array ` `using` `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 `

Output:

```4
```

Time Complexity: O(N), where N is the size of the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :