Answers for "find second largest number in array javascript"

7

find second largest number in array javascript

var secondMax = function (){ 
    var arr = [20, 120, 111, 215, 54, 78]; // use int arrays
    var max = Math.max.apply(null, arr); // get the max of the array
    arr.splice(arr.indexOf(max), 1); // remove max from the array
    return Math.max.apply(null, arr); // get the 2nd max
};
Posted by: Guest on October-14-2020
0

find second largest number in array javascript

var secondMax = function (arr){ 
    var max = Math.max.apply(null, arr), // get the max of the array
        maxi = arr.indexOf(max);
    arr[maxi] = -Infinity; // replace max in the array with -infinity
    var secondMax = Math.max.apply(null, arr); // get the new max 
    arr[maxi] = max;
    return secondMax;
};
Posted by: Guest on September-24-2020
0

javascript find the second highest Element from array

var secondMax = function (){ 
    var arr = [20, 120, 111, 215, 54, 78]; // use int arrays
    var max = Math.max.apply(null, arr); // get the max of the array
    arr.splice(arr.indexOf(max), 1); // remove max from the array
    return Math.max.apply(null, arr); // get the 2nd max
};
Posted by: Guest on May-23-2021
0

find second largest number in array javascript

const arr = ['20','120','111','215','54','78', '120'];   
let intArray = arr.map(Number); // convert into number
intArray = [...new Set(intArray)]; // Remove duplicate numbers
const secondLargestNumber = intArray.sort((a,b) => {
   return b - a;
})[1];
console.log(secondLargestNumber) // 120
Posted by: Guest on June-22-2021
-1

second largest number in array javascript

['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
Posted by: Guest on March-26-2021

Code answers related to "find second largest number in array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language