📌  相关文章
📜  检查数组是否具有等于其余元素的XOR的元素

📅  最后修改于: 2021-05-04 09:20:50             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是检查该数组是否具有等于所有其余元素的XOR的元素。
例子:

方法:首先,对数组所有元素进行XOR并将其存储在变量xorArr中。现在再次遍历整个数组,并为每个元素计算除当前元素arr [i]之外的所有数组元素的异或,即x = xorArr ^ arr [i]
如果x = arr [i],arr [i]是必需元素,因此显示Yes 。如果找不到这样的元素,则打印No。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


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


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


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


Javascript


输出:
Yes