The size of vector res, val, count can at most be size of 2. So it is constant space.

```
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int>res;
if (nums.empty()) return res;
vector<int> val,count;
for (int i=0;i<nums.size();i++){
if (val.empty()){
val.push_back(nums[i]);
count.push_back(1);
}else if (val.size()==1){
if (val[0]==nums[i]){
count[0]++;
}else{
val.push_back(nums[i]);
count.push_back(1);
}
}else { // size == 2
if (val[0]==nums[i]){
count[0]++;
}else if (val[1]==nums[i]){
count[1]++;
}else{
count[0]--;
count[1]--;
if (count[1]==0){val.pop_back();count.pop_back();}
if (count[0]==0){val.erase(val.begin());count.erase(count.begin());}
}
}
}
for (int i=0;i<val.size();i++){
int temp=0;
for (int j=0;j<nums.size();j++){
if (nums[j]==val[i]) temp++;
}
if (temp>nums.size()/3) res.push_back(val[i]);
}
return res;
}
};
```