Given an array **arr[]** of **N** elements, the task is to check if the array has an element which is equal to the XOR of all the remaining elements.

**Examples:**

Input:arr[] = { 8, 2, 4, 15, 1 }

Output:Yes

8 is the required element as 2 ^ 4 ^ 15 ^ 1 = 8.

Input:arr[] = {4, 2, 3}

Output:No

**Approach:** First, take the XOR of all the elements of the array and store it in a variable **xorArr**. Now traverse the complete array again and for every element, calculate the xor of the array elements excluding the current element **arr[i]** i.e. **x = xorArr ^ arr[i]**.

If **x = arr[i]** then **arr[i]** is the required element and hence print **Yes**. If no such element is found then print **No**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if the array ` `// contains an element which is equal to ` `// the XOR of the remaining elements ` `bool` `containsElement(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the XOR of all ` ` ` `// the array elements ` ` ` `int` `xorArr = 0; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `xorArr ^= arr[i]; ` ` ` ` ` `// For every element of the array ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// Take the XOR after excluding ` ` ` `// the current element ` ` ` `int` `x = xorArr ^ arr[i]; ` ` ` ` ` `// If the XOR of the remaining elements ` ` ` `// is equal to the current element ` ` ` `if` `(arr[i] == x) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// If no such element is found ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 8, 2, 4, 15, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `if` `(containsElement(arr, n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function that returns true if the array ` `// contains an element which is equal to ` `// the XOR of the remaining elements ` `static` `boolean` `containsElement(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` ` ` `// To store the XOR of all ` ` ` `// the array elements ` ` ` `int` `xorArr = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `xorArr ^= arr[i]; ` ` ` ` ` `// For every element of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `{ ` ` ` ` ` `// Take the XOR after excluding ` ` ` `// the current element ` ` ` `int` `x = xorArr ^ arr[i]; ` ` ` ` ` `// If the XOR of the remaining elements ` ` ` `// is equal to the current element ` ` ` `if` `(arr[i] == x) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// If no such element is found ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `[] arr = { ` `8` `, ` `2` `, ` `4` `, ` `15` `, ` `1` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `if` `(containsElement(arr, n)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function that returns true if the array ` `# contains an element which is equal to ` `# the XOR of the remaining elements ` `def` `containsElement(arr, n): ` ` ` ` ` `# To store the XOR of all ` ` ` `# the array elements ` ` ` `xorArr ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `xorArr ^` `=` `arr[i] ` ` ` ` ` `# For every element of the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Take the XOR after excluding ` ` ` `# the current element ` ` ` `x ` `=` `xorArr ^ arr[i] ` ` ` ` ` `# If the XOR of the remaining elements ` ` ` `# is equal to the current element ` ` ` `if` `(arr[i] ` `=` `=` `x): ` ` ` `return` `True` ` ` ` ` `# If no such element is found ` ` ` `return` `False` ` ` `# Driver Code ` `arr ` `=` `[` `8` `, ` `2` `, ` `4` `, ` `15` `, ` `1` `] ` `n ` `=` `len` `(arr) ` ` ` `if` `(containsElement(arr, n)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function that returns true if the array ` `// contains an element which is equal to ` `// the XOR of the remaining elements ` `static` `bool` `containsElement(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` ` ` `// To store the XOR of all ` ` ` `// the array elements ` ` ` `int` `xorArr = 0; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `xorArr ^= arr[i]; ` ` ` ` ` `// For every element of the array ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `{ ` ` ` ` ` `// Take the XOR after excluding ` ` ` `// the current element ` ` ` `int` `x = xorArr ^ arr[i]; ` ` ` ` ` `// If the XOR of the remaining elements ` ` ` `// is equal to the current element ` ` ` `if` `(arr[i] == x) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// If no such element is found ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `[] arr = { 8, 2, 4, 15, 1 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `if` `(containsElement(arr, n)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

**Output:**

Yes

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.

## Recommended Posts:

- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Element equal to the sum of all the remaining elements
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Count of elements which are equal to the XOR of the next two elements
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Average of remaining elements after removing K largest and K smallest elements from array
- Check if Sum and XOR of all elements of array is equal
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Check if every index i has an index j such that sum of elements in both directions are equal
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Find last remaining element after reducing the Array
- Count pairs in an array such that both elements has equal set bits
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Construct an array from XOR of all elements of array except element at same index
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Arrange array elements such that last digit of an element is equal to first digit of the next element

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.