[LeetCode] Find the XOR of Numbers Which Appear Twice

3158. Find the XOR of Numbers Which Appear Twice

You are given an array nums, where each number in the array appears either once or twice.

Return the bitwise XOR of all the numbers that appear twice in the array, or 0 if no number appears twice.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int duplicateNumbersXOR(vector<int>& A) {
unordered_map<int,int> us;
int res = 0;
for(auto& x : A) {
if(++us[x] == 2) res ^= x;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/26/PS/LeetCode/find-the-xor-of-numbers-which-appear-twice/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.