Find XOR of all elements in an Array
Last Updated :
04 Jan, 2023
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
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.
Below is the implementation of the above approach:
CPP
// 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 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[0]);
// Function call
cout << xorOfArray(arr, n) << endl;
return 0;
}
C
// 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 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[0]);
// Function Call
printf("%d\n", xorOfArray(arr, n));
return 0;
}
// This code is contributed by phalashi.
Java
// Java program to find the XOR of
// all elements in the array
import java.util.*;
import java.io.*;
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
JavaScript
<script>
// JavaScript program to find the XOR of
// all elements in the array
// Function to find the XOR of
// all elements in the array
function 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>
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Check if Sum and XOR of all elements of array is equal Given an array arr[], the task is to check if sum of all elements of an array is equal to XOR of all elements of array. Example: Input: arr[] = [1, 2] Output: YES Explanation: Sum = (1+2) = 3 XOR = (1^2) = 3 Input: arr[] = [6, 3, 7, 10] Output: NO Explanation: Sum = (6 + 3 + 7 + 10) = 26 XOR = (6 ^
4 min read
Find single in an array of 2n+1 integer elements Given an array with 2n+1 integers, n elements appear twice in arbitrary places in the array and a single integer appears only once somewhere inside. Find the lonely integer with O(n) operations and O(1) extra memory. Examples : Input : { 1, 1, 2, 2, 3, 3, 4, 4, 5} Output : 5 Input : { 7, 9, 6, 8, 3,
4 min read
Bitwise AND of all the elements of array Given an array, arr[] of N integers, the task is to find out the bitwise AND(&) of all the elements of the array. Examples: Input: arr[] = {1, 3, 5, 9, 11} Output: 1 Input: arr[] = {3, 7, 11, 19, 11} Output: 3 Approach: The idea is to traverse all the array elements and compute the bitwise AND f
4 min read
Bitwise AND of all the elements of array Given an array, arr[] of N integers, the task is to find out the bitwise AND(&) of all the elements of the array. Examples: Input: arr[] = {1, 3, 5, 9, 11} Output: 1 Input: arr[] = {3, 7, 11, 19, 11} Output: 3 Approach: The idea is to traverse all the array elements and compute the bitwise AND f
4 min read
Bitwise AND of all the elements of array Given an array, arr[] of N integers, the task is to find out the bitwise AND(&) of all the elements of the array. Examples: Input: arr[] = {1, 3, 5, 9, 11} Output: 1 Input: arr[] = {3, 7, 11, 19, 11} Output: 3 Approach: The idea is to traverse all the array elements and compute the bitwise AND f
4 min read
Make all elements of the Array zero Given, an array A of length N, the task is to make every element of the array equal to zero, by doing some number of moves (possibly zero). In a single move, you can choose two integers l and r such that 1 ≤ l ≤ r ≤ N. Let x = A[l] ^ A[l+1] ^ A[l+2] ... A[r], where ^ represents
6 min read