Simple C++ Solution


  • 36
    X

    The trick is: A^B^A=B

    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            if(nums.empty()) return 0;
            int first = nums[0];
            for(int i = 1; i<nums.size();i++) {
                first = first ^ nums[i];
            }
            return first;
        }
    };

Log in to reply
 

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