We can apply the same idea as easy version of majority element. If we remove three elements with different value at the same time, finally we should filter out the majority elements. So in the first pass, we search for possible majority elements (the number of majority element <3), and then for each candidate, we scan the array again to confirm wether it's majority or not.

*Updated*

Some guys are confused by the first branch, i.e.

```
if(nums[i] == candidate1) count1 ++;
```

Here we don't need to verify the value of count1. Why? Because if count1 is 0, and nums[i] = candidate1,

we can still just add one to count1. It's logically safe.

```
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int array_size = nums.size();
int candidate1, candidate2;
int count1 = 0;
int count2 = 0;
for(int i = 0; i < array_size; i ++){
if(nums[i] == candidate1) count1 ++;
else if(nums[i] == candidate2) count2 ++;
else{
if(count1 && count2) {count1 --; count2 --;}
else if (count1){candidate2 = nums[i]; count2 = 1;}
else {candidate1 = nums[i]; count1 = 1;}
}
}
vector<int> candidate;
if(count1 > 0) candidate.push_back(candidate1);
if(count2 > 0) candidate.push_back(candidate2);
vector<int> result;
for(int i = 0; i < candidate.size(); i ++){
int count = 0;
for(int j = 0; j < array_size; j ++){
if(nums[j] == candidate[i]) count ++;
}
if(count > array_size/3) result.push_back(candidate[i]);
}
return result;
}
};
```