C++ solution bit manipulation


  • 2
    K
    class Solution {
    public:
    vector<int> singleNumber(vector<int>& nums) {
        int a=0, b=0, two_xor=0, set_bit, sum=0;
        vector<int>res;
        for(int i=0;i<nums.size();i++)
        {
            two_xor^=nums[i];
        }
        set_bit=two_xor& ~(two_xor- 1);
    
        for(int j=0;j<nums.size();j++)
        {
            if(nums[j] & set_bit)a=a^nums[j];
            else b=b^nums[j];
        }
        res.push_back(a);
        res.push_back(b);
        return res;
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.