> 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/137.-single-number-ii.md).

# 137. Single Number II

https\://leetcode.com/problems/single-number-ii/description/

除了一个数出现一次外其它数出现3次，找出出现一次的数。针对所有数出现K次，只有一个数num出现M(M%K !=0)次的问题可以对每一位把该位的值加起来然后%K，得到M个num在该位的和 %K的结果，再除以M % K就是num在该位的值。

```
/*
 * @lc app=leetcode id=137 lang=cpp
 *
 * [137] Single Number II
 */

// @lc code=start
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for (int i = 0; i < 32; ++i) {
            int r = 0;
            for (const auto num : nums) {
                r += (num >> i) & 1;
            }
            res |= (r % 3) << i;
        }
        return res;
    }
};
// @lc code=end


```
