Find Non-Repeating Number
Finds the number that appears only once in an array where every other number appears twice.
- bit-manipulation
- xor
Code
#include <vector>
int find_non_repeating(const std::vector<int> nums) {
int result = 0;
for (const int num : nums) {
result ^= num;
}
return result;
}Usage
std::vector<int> nums = {4, 1, 2, 1, 2};
find_non_repeating(nums); // Returns: 4