Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.

&nbsp;
Example 1:

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


Example 2:

Input: nums = [1,2]
Output: 2
Explanation:
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.


Example 3:

Input: nums = [2,2,3,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2's are counted together since they have the same value).
The third distinct maximum is 1.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 104
	-231 &lt;= nums[i] &lt;= 231 - 1


&nbsp;
Follow up: Can you find an O(n) solution?
class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        nums_set = set(nums)
        sorted_set = sorted(nums_set)
        return sorted_set[-3] if len(nums_set) >2 else sorted_set[-1]
    
    
            #use set() to remove dups
            #if len of nums after dups have been removed is at least 2, a third max val must exist
            #if not, just return the max
        
        
        #you can do it in 1 line like this but then you have to call the same functions repeatedly
        #return sorted(set(nums))[-3] if len(set(nums)) > 2  else sorted(set(nums))[-1]
    
 
    
class Solution {
    public int thirdMax(int[] nums) {
        Integer first = null, second = null, third = null;
        for(Integer num: nums){
            if(num.equals(first) || num.equals(second) || num.equals(third)) continue;
            if(first == null || num > first){
                third = second;
                second = first;
                first = num;
                continue;
            } 
            if(second == null || num > second){
                third = second;
                second = num;
                continue;
            } 
            if(third == null || num > third){
                third = num;
            }
        }
        return (third != null) ? third : first;
    }
}
class Solution {
public:
 int thirdMax(vector<int>& nums) {
           set<int>s;
        for(int i=0;i<nums.size();i++){
            s.insert(nums[i]);
        }
        if(s.size()>=3){   // when set size >=3 means 3rd Maximum exist(because set does not contain duplicate element)
            int Third_index_from_last=s.size()-3;
            auto third_maximum=next(s.begin(),Third_index_from_last);
            return *third_maximum;
        }
            return *--s.end(); // return maximum if 3rd maximum not exist
    }
};
var thirdMax = function(nums) {
    nums.sort((a,b) => b-a);
	//remove duplicate elements
    for(let i=0; i<nums.length;i++){
        if(nums[i] === nums[i+1]){
            nums.splice(i+1, 1);
            i--
        }
    }
    
    return nums[2] !=undefined?nums[2]: nums[0]
};
Third Maximum Number