8.(异或)一个数组中有两种数出现了奇数次,其他数都出现了偶数次,怎么找到这两个数
一个数组中有两种数出现了奇数次,其他数都出现了偶数次,怎么找到这两个数?
int* a =new int[2];
int* GetOddTwo(int arr[], int size)
{
if (!arr) return NULL;
int eor = 0;
for (int i = 0; i < size-1; ++i)
{
eor ^= arr[i];
}
int rightOne = eor & (~eor + 1);
int eor1 = 0;
for (int i = 0; i < size-1; ++i)
{
if (arr[i] & rightOne == 0)
{
eor1 ^= arr[i];
}
}
a[0] = eor1;
a[1] = eor ^ eor1;
return a;
}