Problem:
Given an array of integers, every element appears three times
except for one, which appears exactly once. Find that single one.
Solution:
class Solution {
public:
int singleNumber(vector<int>& nums) {
if(nums.size() == 1) return nums[0];
sort(nums.begin(), nums.end());
int n = nums[0], cnt = 1;
for(int i = 1; i < nums.size(); i++){
if(nums[i] == n)
cnt++;
else{
if(cnt == 3){
n = nums[i];
cnt = 1;
}else
return nums[i - 1];
}
}
return nums.back();
}
};