AFFILIATE LINKS
Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard
Here is the full implementation.
/**
* @param {number[]} nums
* @return {number}
*/
let thirdMax = function(nums) {
let thirdMax = -Infinity;
let secondMax = -Infinity;
let max = nums[0];
for (let i = 1; i < nums.length; i++) {
let num = nums[i];
if (num > max) {
thirdMax = secondMax;
secondMax = max;
max = num;
} else if (num < max && num > secondMax) {
thirdMax = secondMax;
secondMax = num;
} else if (num < secondMax && num > thirdMax) {
thirdMax = num;
}
}
return thirdMax === -Infinity ? max : thirdMax;
};