Sort by

recency

|

1466 Discussions

|

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

    }

  • + 0 comments

    In JS

    function getSecondLargest(nums) {
        // Complete the function
        let dupl = [...new Set(nums)];
    let arr = dupl.sort((a,b)=> a-b);
      
        let res = arr[arr.length-2]
        return res; 
    }