LeetCode algorithm_C++——Numbers that appear only once

You are given a non-empty integer array nums. Except for an element that appears only once, each element appears twice. Find the element that appears only once.

You must design and implement a linear-time algorithm to solve this problem that uses only constant extra space.

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

Example 2:
Input: nums = [4,1,2,1,2]
Output: 4

Example 3:
Input: nums = [1]
Output: 1

int singleNumber(vector<int>& nums) {
    
    
    int tmp = 0;
    for (auto e : nums)
    {
    
    
        tmp ^= e;
    }
    return tmp;
}

Guess you like

Origin blog.csdn.net/weixin_43945471/article/details/132722971