For this question, "Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once. "
I tried the code given at http://www.geeksforgeeks.org/find-the-element-that-appears-once/. But, I get an incorrect answer of 1 when input is 3,3,1,3,6,1,6,7,1.
Please help me in finding the mistake I'm doing.
#include<iostream>
using namespace std;
int getSingleOccurrence(int nArray[9], int n){
int ones = 0, twos=0;
int common_bit_mask=0;
for(int i=0;i<n;i++){
twos|= ones & nArray[i];
ones^=nArray[i];
common_bit_mask = ~(ones & twos);
ones&=common_bit_mask;
twos&=common_bit_mask;
}
return ones;
}
int main(){
int nArray[]={3,3,1,3,6,1,6,7,1};
cout<<getSingleOccurrence(nArray,9);
return 0;
}