```
int thirdMax(vector<int>& nums) {
vector<long long> vec = { LONG_MIN, LONG_MIN, LONG_MIN };
for (int i = 0; i < nums.size(); i++) {
if (nums[i]>vec[0] && nums[i]!=vec[1] && nums[i]!=vec[2]) {
vec[0] = nums[i];
sort(vec.begin(), vec.end());
}
}
return vec[0] == LONG_MIN ? vec[2] : vec[0];
}
```

We have a sort inside the loop, but time complexity is still O(N) as we're sorting only 3 numbers no matter the size of the input.