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 :: javascript filter and order 
Javascript :: js alert new line 
Javascript :: jest debugger node 
Javascript :: javascript shift everything in array to the right 
Javascript :: how to import a json string from a file in typescript 
Javascript :: js for in 
Javascript :: js hoisting 
Javascript :: How to block ctrl+shift+j using javascript 
Javascript :: joi validation enum 
Javascript :: array fill 
Javascript :: how to disable link in react 
Javascript :: square element in array 
Javascript :: insert element at beginning of array javascript 
Javascript :: js get current year last 2 digits substring 
Javascript :: Uncaught TypeError: $(...).jstree is not a function 
Javascript :: calling angular component method in service 
Javascript :: js add function to array 
Javascript :: delete last character from string js 
Javascript :: scroll value bottom js 
Javascript :: gitignore subfolders 
Javascript :: slide hide animaition in react 
Javascript :: javascript promise.all 
Javascript :: reverse array js 
Javascript :: ordenar un array de menor a mayor 
Javascript :: mongoose number bigger 
Javascript :: toastr options 
Javascript :: how to swap two elements in an array javascript 
Javascript :: javascript bind this to anonymous function 
Javascript :: react route path exact 
Javascript :: jquery global variable 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =