• + 0 comments

    So, O(n) is the best we could expect here.

    function getSecondLargest(nums) {
        // Complete the function
        let max = -Infinity, sndMax = -Infinity;
        for (let num of nums) {
            if (num > max) {
                sndMax = max;
                max = num;
            } else if (num > sndMax && num !== max) {
                sndMax = num;
            }
        }
        return sndMax;
    }