Given an array of integers, every element appears twice except for one. Find that single one.
Solution:
1 2 3 4 5 6 7 8 9 10 11 |
class Solution { public: int singleNumber(vector<int>& nums) { int A = nums[0]; for(int i = 1; i < nums.size();i++) { A = A ^ nums[i]; } return A; } }; |
C++:
return accumulate(nums.cbegin(), nums.cend(), 0, std::bit_xor());
Python:
return reduce(operator.xor, nums)