Simple C++ solution, 20ms


  • 0
    D

    For every pair (x, x), x ^ x = 0, and 0 is the identity for ^, and ^ is commutative. So reduce the entire list of numbers with ^ and only the missing number remains.

    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            int result = 0;
            for (int n : nums) {
                result ^= n;
            }
            return result;
        }
    };

Log in to reply
 

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