Created
June 22, 2020 14:22
-
-
Save SuryaPratapK/adc19b0c553dcb17dd6cafe2485fabdc 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
class Solution { | |
public: | |
int singleNumber(vector<int>& nums) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
//Using XOR + AND combination | |
int ones = 0; | |
int twos = 0; | |
for(auto ele: nums) | |
{ | |
ones = (ones^ele) & (~twos); | |
twos = (twos^ele) & (~ones); | |
} | |
return ones; | |
/* | |
//Counting SET-BIT | |
int n=nums.size(); | |
unsigned int ans=0,shift=1; | |
for(int i=0;i<32;++i) | |
{ | |
int count=0; | |
for(auto ele: nums) | |
{ | |
if(ele & shift) | |
count+=1; | |
} | |
if(count%3 != 0) | |
ans += shift; | |
shift*=2; | |
} | |
return ans; | |
*/ | |
/* | |
//SORTING | |
int n=nums.size(); | |
if(n<3) | |
return nums[0]; | |
sort(nums.begin(),nums.end()); | |
if(nums[0]!=nums[1]) | |
return nums[0]; | |
int i=1; | |
while(i<n) | |
{ | |
if(nums[i]!=nums[i-1]) | |
return nums[i-1]; | |
i+=3; | |
} | |
return nums[n-1]; | |
*/ | |
} | |
}; |
first code is not working for all the given input as I submit the code on interviewbit
I think it's only applicable for sorted array.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
first code is not working for all the given input as I submit the code on interviewbit