[bit] [array] find element that appears once. bit manipulate.
/*
Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once. Expected time complexity is O(n) and O(1) extra space.
Examples:
Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3}
Output: 2
*/
int find_number(int A[], int N) {
int res = 0;
for(int i=0; i<32; i++) {
int mask = 1 << i;
int sum = 0;
for(int j=0; j<N; j++) {
sum += A[j] & mask;
}
if(sum % 3 != 0) {
res |= 1 << i;
}
}
return res;
}