O(n) short and simple


  • 0
    var f = (v1 = -Infinity, v2 = -Infinity) => v2 - v1;
    
    var thirdMax = function(nums) {
        let m1, m2, m3;
        nums.forEach((v) => {
            if ([ m1, m2, m3 ].indexOf(v) === -1)
                [ m1, m2, m3 ] = [ m1, m2, m3, v ].sort(f);
        });
        return m3 != null ? m3 : m1;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.