136. 只出现一次的数字 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public:
int singleNumber(vector<int>& nums) {
int n=nums.size();
sort(nums.begin(),nums.end());
int i=0,j=1;
while(i<n&&j<n){
if(nums[i]==nums[j]){
i=j+1;
j=i+1;
}
else break;
}
return nums[i];
}
};
异或:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=0;
for(int i=0;i<nums.size();i++){
res=res^nums[i];
}
return res;
}
};