<p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The first distinct maximum is 2.
The second distinct maximum is 1.
The third distinct maximum does not exist, so the maximum (2) is returned instead.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,2,3,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2&#39;s are counted together since they have the same value).
The third distinct maximum is 1.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Can you find an <code>O(n)</code> solution?
class Solution {
public:
    int thirdMax(vector<int>& nums) {
        set<int> s; 
        for(auto num:nums) s.insert(num);
        auto it = s.rbegin();
        if(s.size() > 2) advance(it,2);
        /*cout<<*it<<endl;
        // Check the trivial cases
        if(nums.size()==1) return nums[0];
        if(nums.size()==2) return nums[0]>nums[1]?nums[0]:nums[1];
        
        //arrange them
        int temp,result;
        for(int i =0 ;i<nums.size();++i){
            for(int j = i+1;j<nums.size();++j){
                if(nums[i]>nums[j]) continue;
                temp = nums[i];
                nums[i]=nums[j];
                nums[j]=temp;
            }
        }
        //Create array with uniques
        vector<int> uniq;
        uniq.push_back(nums[0]);
        for(int i = 1;i<nums.size();++i){
            if(nums[i-1]!=nums[i]) uniq.push_back(nums[i]);
        }
        // If size is less than 3, return the max
        switch(uniq.size()){
            case 1: result = uniq[0];break;
            case 2: result = uniq[0]>uniq[1]?uniq[0]:uniq[1];break;
            default: result = uniq[2];
        }
*/
        return *it;
    }
};
13400