Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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
};
Comment

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
};
Comment

second largest number in array javascript

['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
Comment

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;
};
Comment

find the second largest number in an array javascript

function largestOfFour(mainArray) {
  return mainArray.map(function (subArray){
    return subArray.reduce(function (previousLargestNumber, currentLargestNumber) {
      return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber;
    }, 0);
  });
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Comment

find the second largest number in array javascript

const input = ['20', '120', '111', '215', '54', '78'];
const secondSort = input.sort(function (a, b) { return b - a}[1])
console.log("===> :: secondSort", secondSort);
Comment

PREVIOUS NEXT
Code Example
Javascript :: js .substring 
Javascript :: ** javascript Exponentiation 
Javascript :: delete from list javascript 
Javascript :: jquery change h1 text 
Javascript :: hello world in javascript 
Javascript :: crypt a string jquery 
Javascript :: Detecting by how much user has scrolled | get how much i scroll in js 
Javascript :: price range slider bootstrap 4 
Javascript :: string into json javascript 
Javascript :: find an object from array of objects javascript 
Javascript :: create object from array 
Javascript :: how to detect click outside div 
Javascript :: difference let and var 
Javascript :: add next to react 
Javascript :: jest render target container is not a dom element 
Javascript :: javascript button click event 
Javascript :: how to disable strict mode on object in javascript 
Javascript :: unix timestamp js 
Javascript :: html canvas not clearing 
Javascript :: vscode js intellisence not working 
Javascript :: create secure jwt secret key using node crypto 
Javascript :: how to set default value in input field in angularjs 
Javascript :: angularjs onclick disable button click 
Javascript :: reverse a string while keeping spaces in javascript 
Javascript :: get param from url jquery 
Javascript :: ${} js 
Javascript :: preventdefault javascript 
Javascript :: fibonacci recursion 
Javascript :: js nuxt read/set cookie 
Javascript :: node.js check if a remote URL exists 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =