Sort by

recency

|

1477 Discussions

|

  • + 0 comments

    function getSecondLargest(nums) {

    // Complete the function
    
    let max=nums[0];
    let secondmax = 0;
    
    for (let i=0;i<nums.length-1;i++)
    {
        if(nums[i]>nums[i+1] && nums[i]>max){
            max = nums[i];
        }
        else if(nums[i+1]>max ){
            max=nums[i+1];
        }
    }
    
    //10,
    for(let i=0;i<nums.length-1;i++){
        if(nums[i]>nums[i+1] && 
        Number(nums[i])!=Number(max) && Number(nums[i+1])!=
        Number(max) && nums[i]>secondmax)
        {
            secondmax=nums[i];
    
        }
    
        else if(Number(nums[i+1])!=
        Number(max)&& nums[i+1]>secondmax){secondmax=nums[i+1];}
    
    
    }
    return secondmax;
    

    }

  • + 0 comments

    Says constraint of n can be 1, but it MUST be at least 2 for this exercise.

  • + 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; }