Using XOR operator for finding duplicate elements in a array fails in many cases

前端 未结 6 2066
野的像风
野的像风 2021-02-01 10:36

I came across a post How to find a duplicate element in an array of shuffled consecutive integers? but later realized that this fails for many input.

For ex:
a

6条回答
  •  無奈伤痛
    2021-02-01 11:10

    //There i have created the program to find out the duplicate element in array.  Please edit if there are required some changes.  
    int main()  
    {  
        int arr[] = {601,602,603,604,605,605,606,607};  
        //int arr[] = {601,601,604,602,605,606,607};  
        int n= sizeof(arr)/sizeof(arr[0]);  
    
        for (int i = 0; i < n; i++)  
        {  
            for (int j = i+1; j < n; j++)  
            {  
                 int res = arr[i] ^ arr[j];  
    
                 if (res == 0)  
                 {  
                     std::cout<< "Repeated Element in array = "<

    //OR You can use HashTable and Hash Function when you enter the same
    value into the hash table that time you can make count if its greater than
    one value at particular index of HashTable then you can say that there are repeated value in the array.

提交回复
热议问题