Learned from others. Calculate the single number bit by bit. Add the same bit of all numbers and mod 3 to get the bit of that single number.
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=0;
for(int i=0;i<32;i++)
{
int sum=0;
for(int j=0;j<nums.size();j++)
sum+=(nums[j]>>i)&1;
res|=(sum%3)<<i;
}
return res;
}
};