Skip to content

Instantly share code, notes, and snippets.

@HarshKumarChoudary
Created June 10, 2021 10:31
Show Gist options
  • Save HarshKumarChoudary/583f5b6c95e2fcf1ebc5645129b2b58f to your computer and use it in GitHub Desktop.
Save HarshKumarChoudary/583f5b6c95e2fcf1ebc5645129b2b58f to your computer and use it in GitHub Desktop.
/* suppose the problem says tha we are given an array with only one unique element and rest all elements have their duplicates and we have to find the
unique element in O(n) time and O(1) space complexity then we can use the idea of XOR as shown below:
*/
#include<iostream>
using namespace std;
int main(){
int ans=0;
for(int i=0;i<n;++i)
ans ^= arr[i];
// this will find the unique element in the array in O(1) space complexity
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment