Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
include<stdio.h>
int singleNumber(int A[], int n) {
int bits[32];
int i,j,result = 0;
for(j = 0; j < 32; j++) bits[j] = 0;
for(i = 0; i < n; i++){
for(j = 0; j < 32; j++){
if((1 & (A[i] >> j)) == 1) bits[j]++;
}
}
for(j = 0; j < 32; j++) {
if(bits[j] % 3 != 0){
result += (1 << j);
}
}
return result;
}
void main(){
int A[]={2, 2, 2, 3};
int n = 4;
printf("%d\n", singleNumber(A, n));
}