find unique number among array | Revo
Given an array, find the unique number which shows only once whereas other numbers show 3 times.
Given an array, find the unique number which shows only once whereas other numbers show 3 times.
public static int findUniq(int[] arr) {
int[] tmp = new int[32];
for(int i = 0; i< arr.length; i++) {
for(int j = 31; j >= 0 && arr[i] != 0; j--) {
tmp[j] += arr[i]%2;
arr[i] = arr[i] >> 1;
}
}
int res = 0;
int product = 1;
for(int j = 0; j<=31; j++) {
tmp[31-j] = tmp[31-j]%3;
res += tmp[31-j]*product;
product *= 2;
}
return res;
}
Read full article from find unique number among array | Revo