Created
June 10, 2021 10:31
-
-
Save HarshKumarChoudary/583f5b6c95e2fcf1ebc5645129b2b58f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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