Sort by

recency

|

1475 Discussions

|

  • + 0 comments

    **Simple and Short Solution **

    function getSecondLargest(nums) {
    nums.sort((a,b) => a-b )
    const c = [...new Set(nums)]
    return c.at(-2)
    }
    
  • + 0 comments

    function getSecondLargest(nums) { // Complete the function let fun = nums.sort(function(a,b){return b-a}) let rem = [] for (let i = 0;i

    }

  • + 0 comments

    function getSecondLargest(nums) { let maxVal = Math.max(...nums); nums = nums.filter(item => item !== maxVal) let maxVal2 = Math.max(...nums); return maxVal2; }

  • + 0 comments

    function getSecondLargest(nums) { nums.sort((a,b)=>{ return b-a;
    }) for(var i=0; i nums[i+1]) return nums[i+1]; } }

  • + 0 comments

    Simple solution in single loop

    function getSecondLargest(nums) {
        // Complete the function
        let largest = nums[0];
        let secondLargest= nums[0];
        let n= nums.length ;
        
        for(let i=0; i<n; i++){
            if(nums[i]>largest){
                secondLargest=largest;
                largest= nums[i];
            }else if(nums[i]>secondLargest && nums[i]!= largest){
                secondLargest= nums[i];
            }
        }
        return secondLargest;
    }