一个全是数字的大数组,除了其中一个数字出现2次外,其余的数字都出现了3次。如何找出那个只出现了两次的数字?
其中ones记录了所有出现了模3余1次的bit,twos记录的是余2的。not_threes是当一个bit出现第3次的时候,把它清掉。
public class Test3in2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int ones=0;
int twos=0;
int not_threes=0;
int a[]={22,33,22,66,66,33,33,66,55,22,55,55,121,121};
for(int i=0;i<a.length;i++){
twos|=ones&a[i];
ones^=a[i];
not_threes=~(ones&twos);
ones&=not_threes;
twos&=not_threes;
}
System.out.println("222===="+twos);
System.out.println("111===="+ones);
}
}