Leetcode 260 Solution

This article provides solution to leetcode question 260 (single-number-iii)

https://leetcode.com/problems/single-number-iii

Solution

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int diff = 0;

        for (int i = 0; i < nums.size(); i++)
            diff ^= nums[i];

        diff &= -diff;

        vector<int> res(2, 0);

        for (int i = 0; i < nums.size(); i++)
            if (nums[i] & diff)
                res[0] ^= nums[i];
            else
                res[1] ^= nums[i];

        return res;
    }
};