find two largest numbers in array javascript code example
Example 1: how to find the smallest two numbers in an array javascript
function sumTwoSmallestNumbers(numbers) {
numbers = numbers.sort((a, b) => {
return a - b; });
};
Example 2: find second largest number in array javascript
var secondMax = function (){
var arr = [20, 120, 111, 215, 54, 78];
var max = Math.max.apply(null, arr);
arr.splice(arr.indexOf(max), 1);
return Math.max.apply(null, arr);
};
Example 3: find second largest number in array javascript
var secondMax = function (arr){
var max = Math.max.apply(null, arr),
maxi = arr.indexOf(max);
arr[maxi] = -Infinity;
var secondMax = Math.max.apply(null, arr);
arr[maxi] = max;
return secondMax;
};