10 C++ lines solution using accumulate and lambda


  • 1
    T
    vector<int> singleNumber(vector<int>& nums) {
            int acc = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
            int mask = acc & ~(acc-1);
            vector<int> output{
                accumulate(nums.begin(), nums.end(), 0,
                    [mask](int acc, int elem) {return elem & mask ? acc ^ elem : acc;}),
                accumulate(nums.begin(), nums.end(), 0,
                    [mask](int acc, int elem) {return elem & mask ? acc : acc ^ elem;})
            };
            return output;
     }

Log in to reply
 

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