```
class Solution {
public:
int thirdMax(vector<int>& nums) {
vector<long> cands { (long)INT_MIN - 1, (long)INT_MIN - 1, (long)INT_MIN - 1 };
for (long it : nums) {
if (it == cands[0] || it == cands[1] || it == cands[2]) continue;
if (it < cands[2]) continue;
for (auto iit = cands.begin(); iit != cands.end(); ++iit) {
if (it > *iit) {
cands.insert(iit, it);
break;
}
}
cands.pop_back();
}
return (cands[2] == (long)(INT_MIN) - 1) ? (int)cands[0] : (int)cands[2];
}
};
```