C++ implementation using XOR


  • 18
    T
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            // XOR (^) is both commutative and associative 
            // The numbers which appear twice will be cancelled
            // Only the number that appear twice survive 
            int value = 0;
            int i, n;
            n = nums.size();
            for(i=0; i<n; i++)
                value = value ^ nums[i];
            return value;
        }
    };

Log in to reply
 

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