How to get the second largest element from an array in javascript

I have an integer array like this:

arr[20,120,111,215,54,78]; 

I need a function that takes an array as an argument and returns the second largest element of this array.

+9
source share
7 answers
 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 }; 

demo

UPDATE

As davin pointed out , performance can be improved by not splicing, but temporarily replacing the -Infininty maximum value:

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

In any case, IMHO the best algorithm is Jack. 1 pass, with the transition to the number. The mine is just short, using the built-in methods and just wanted to present it as an alternative to show all the different ways to achieve the goal.

+21
source

The simplest implementation without changing the original array:

 var arr = Array('20','120','111','215','54','78'), biggest = -Infinity, next_biggest = -Infinity; for (var i = 0, n = arr.length; i < n; ++i) { var nr = +arr[i]; // convert to number first if (nr > biggest) { next_biggest = biggest; // save previous biggest value biggest = nr; } else if (nr < biggest && nr > next_biggest) { next_biggest = nr; // new second biggest value } } console.log(next_biggest); 

Demo

+25
source

The simplest solution is to sort:

 // here your array : var stringArray = new Array('20','120','111','215','54','78'); // let convert it to a real array of numbers, not of strings : var intArray = stringArray.map(Number); // now let sort it and take the second element : var second = intArray.sort(function(a,b){return ba})[1]; 

If you don’t need the simplest, but the fastest (you probably don’t need it), you will have to write a for loop and store the two largest elements during the loop.

+12
source

First sort it back, and then get the second element:

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

Obviously also works with strings.

+2
source

You can try the following:

 function second_highest(arr) { var second_highest = arr.sort(function(a, b) { return b - a; })[1]; return second_highest; } 
+1
source

Sort the array and then return the second index.

 var arr = ['20','120','111','215','54','78']; arr.sort(function(a,b){ return ba; }); console.log(arr[1]); 
0
source

Sort your array from smallest to largest, then grab the second from the end with .length-2

 var myArray =['20','120','111','215','54','78']; var secondLargest = myArray.sort(function(a,b){return a - b})[myArray.length-2]; alert(secondLargest); //120; 
0
source

Source: https://habr.com/ru/post/981398/


All Articles