Javascirpt Solution


  • 0
    T

    `
    var thirdMax = function(nums) {

    //2. iterate; keep track of largest, second largest, third largest
    
    
    var largest = nums[0];
    var third;
    var second;
    
    for(var i = 1; i < nums.length; i++){
       
       if(nums[i] === largest || nums[i] === second || nums[i] === third) continue;
       if(nums[i] > largest){
           var temp1 = largest;
           var temp2 = second;
           largest = nums[i];
           second = temp1;
           third = temp2;
       }else if(nums[i] > second  || !second){
           third = second;
           second = nums[i];
           
       } else if(nums[i] > third || !third){
           third = nums[i];
       }
    }
    
    if(!third) return largest;
    return third;
    

    };

    `


Log in to reply
 

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