Stay button 136 the number of questions arise only once

Stay button 136 the number of questions arise only once

Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears twice. To find out that only appears once in the elements.

Description:

Your algorithm should have linear time complexity. You can not use the extra space to achieve it?

Example 1:

Input: [2,2,1]
Output: 1
Example 2:

Input: [4,1,2,1,2]
Output: 4

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

Guess you like

Origin www.cnblogs.com/woodjay/p/12398257.html