Sort by

recency

|

1467 Discussions

|

  • + 0 comments
    function getSecondLargest(nums) {
        const uniqueNums = nums.filter((value, idx, self)=> self.indexOf(value) === idx);
        return uniqueNums.sort((a,b)=>b-a)[1];
    }
    
  • + 0 comments

    THIS IS MINE

    function getSecondLargest(nums) {
        return Array.from(new Set(nums)).sort((a, b) => b - a) [1];
    }
    
  • + 2 comments
    function getSecondLargest(nums) {
        const uniqueNums = [...new Set(nums)];
        uniqueNums.sort((a, b) => b - a);
        return uniqueNums[1];
    }
    
  • + 0 comments
    function getSecondLargest(nums) {
        return (Array.from(new Set(nums)).sort((a,b) => a - b).reverse())[1]
    }
    
  • + 0 comments

    function getSecondLargest(nums) {

    //! Solution 1
    // const distinctNums = [...new Set(nums)];
    
    // distinctNums.sort((a,b)=> a-b)
    
    // return distinctNums[distinctNums.length -2]
    
    //! Solution 2
    
    let largest = nums[0],
        secondLargest = nums[0];
    
    for (let i = 1; i < nums.length; i++) {
        if (nums[i] > largest) {
            secondLargest = largest;
            largest = nums[i];
        } else if (nums[i] > secondLargest && nums[i] < largest) {
            secondLargest = nums[i];
        }
    }
    
    return secondLargest;
    

    }