JaveScript solution


  • 0
    X
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var thirdMax = function(nums) {
        var set = new Set(nums);
        var newArray = Array.from(set);
    
        //排序
        newArray.sort(function(a, b){
            if (a < b) {
                return -1;
            }
            if (a > b) {
                return 1;
            }
            return 0;
        });
    
        if(newArray.length < 3){
            return newArray[newArray.length - 1];
        }else{
            return newArray[newArray.length - 3];
        }
    };
    
    

Log in to reply
 

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