我们可以使用位操作找到 0 在数组中是否出现奇数次吗

Can we find if 0 occurs odd number of times in an array using bit manipulation

我知道,对一个整数数组的所有元素进行异或运算,该数组包含除 1 个元素出现偶数次以外的所有元素,给出出现奇数次的数。

示例

{ 1, 1, 2, 2, 3 }
1 ^ 1 ^ 2 ^ 2 ^ 3 = 3;

^ 是异或

如果出现奇数次的数是0怎么办?
{ 1, 1, 2, 2, 0 }

1 ^ 1 ^ 2 ^ 2 ^ 0 = 0    // Both give
1 ^ 1 ^ 2 ^ 2 = 0        // same answer  

如何确认0出现奇数次
PS :希望答案代码在 C/C++

我们称 N 为数组中的元素数:

  • If (N is even) AND (XORing all elements ==0) -> 所有元素出现偶数次
  • if (N is odd) AND (XORing all elements ==0) -> 你的单个元素为零。

这是check if an integer is even or odd in C / C++

的方法