260. single number iii

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class  {
public:
vector<int> singleNumber(vector<int>& nums) {
int temp=0;int a=0,b=0;
for(int i=0;i<nums.size();i++){
temp^=nums[i];
}
int temp1=temp&~(temp-1);
for(int i=0;i<nums.size();i++){
if(temp1&nums[i]){
a^=nums[i];
}else{
b^=nums[i];
}
}
return vector<int>({a,b});
}
};