aboutsummaryrefslogtreecommitdiff
path: root/store/works/solutions/leetcode/cpp/260-2.cpp
blob: 336d9e1e78ccb2c00f485123641a461d0a3c6a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>

using std::vector;

class Solution
{
public:
    vector<int> singleNumber(vector<int> &nums)
    {
        int diff = 0;
        for (auto i : nums)
        {
            diff ^= i;
        }

        int mask = diff & (-diff);

        int result = 0;
        for (auto i : nums)
        {
            result ^= (i & mask) ? i : 0;
        }

        return {result, result ^ diff};
    }
};