because the xor has three properties. 1,0^a=a 2,a^b=b^a 3,a^a=0
so swapping the number would not change the output at the end, we can see the list as all the same numbers are adjacent. the same numbers would be 0 after xor. the one remaining would be the answer.

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

I think the code if(nums.length==1){} is not necessary. Because if the length of an array is 1, the loop part will not be conducted (For the first loop, i cannot satisfy 1<nums.length), and the return is still nums[0]'