package pers.lyt.java;
public class Offer56_NumberAppearingOnce {
public int findNumberAppearingOnce(int[] arr) {
if (arr == null || arr.length <= 0)
throw new RuntimeException();
int[] bitSum = new int[32];
for (int i = 0; i < arr.length; i++) {
int bitMask = 1;
for (int j = 31; j >= 0; j--) {
int bit = arr[i] & bitMask;
if (bit != 0)
bitSum[j] += 1;
bitMask = bitMask << 1;
}
}
int result = 0;
for (int i = 0; i < 32; i++) {
result = result << 1;
result += (bitSum[i] % 3);
}
return result;
}
public int singleNumber(int[] nums) {
int[] bit = new int[32];
for (int j = 0; j < 32; j++) {
for (int i = 0; i < nums.length; i++) {
int num = nums[i] >> j;
bit[j] += num & 1;
}
}
int result = 0;
for (int i = 31; i >= 0; i--) {
result <<= 1;
result += bit[i] % 3;
}
return result;
}
public static void main(String[] args) {
Offer56_NumberAppearingOnce demo = new Offer56_NumberAppearingOnce();
int[] arr = new int[] { 2, 2, 2, 5, 3, 3, 3, 6, 6, 6 };
int appearingOnce = demo.findNumberAppearingOnce(arr);
System.out.println(appearingOnce);
}
}