> For the complete documentation index, see [llms.txt](https://hao-fu-1.gitbook.io/oj/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hao-fu-1.gitbook.io/oj/bit-manipulation/xor/260.-single-number-iii.md).

# 260. Single Number III

一组数中有两个出现一次，其余出现两次，让返回两个出现一次的数。两两异或能把有重复的抵消，剩下的是两个出现一次的相XOR的结果。XOR结果中的1表示两个结果在该位不一样，因此把原来的数组与这位相与，也就是res两个结果分在了两组，在每组内做XOR即结果。

```
/*
 * @lc app=leetcode id=260 lang=cpp
 *
 * [260] Single Number III
 */

// @lc code=start
class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int res = 0;
        for (const auto num : nums) {
            res ^= num;
        } 
        res &= -res;
        vector<int> r(2, 0);
        for (const auto num : nums) {
            if ((num & res) == 0) {
                r[0] ^= num;
            } else {
                r[1] ^= num;
            }
        }
        return r;
    }
};
// @lc code=end
```
