My 5 line C++ solution O(n) time and O(1) space complexity


  • 0
    C

    Use that XOR of 2 similar elements is 0 (c^c=0). So, after XORing all the elements which repeat twice , and then XOR the result with the unique element to get the answer.

            int x=nums[0];
            for(int i=1;i<nums.size();i++)
            {
                x=x^nums[i];
            }
            return x;

Log in to reply
 

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