javascript 如何从javascript中的数组中获取第二大元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17039770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the second largest element from an array in javascript
提问by Amar Banerjee
I have an integer array like this :
我有一个这样的整数数组:
arr[20,120,111,215,54,78];
I need a function taking an array as its argument and returning the second largest element of that array.
我需要一个函数,将数组作为参数并返回该数组的第二大元素。
回答by Matyas
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
};
UPDATE
更新
As pointed out by davinthe performance could be enhanced by not doing a splice but temporarilyreplacing the max value with -Infininty
:
正如davin所指出的,可以通过不进行拼接而是暂时用以下内容替换最大值来提高性能-Infininty
:
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;
};
Anyway, IMHO the best algorithm is Hyman's. 1 pass, with conversion to number. Mine is just short, using builtin methods and only wanted to provide it as an alternative, to show off all the different ways you can achieve the goal.
无论如何,恕我直言,最好的算法是Hyman的。1 通过,转换为数字。我的很短,使用内置方法,只想提供它作为替代方案,以炫耀您可以实现目标的所有不同方法。
回答by Ja?ck
The most straightforward implementation, without modifying the original array, is to iterate and track the biggest and next biggest:
最直接的实现,不修改原数组,就是迭代跟踪最大和次大:
function nextBiggest(arr) {
let max = -Infinity, result = -Infinity;
for (const value of arr) {
const nr = Number(value)
if (nr > max) {
[result, max] = [max, nr] // save previous max
} else if (nr < max && nr > result) {
result = nr; // new second biggest
}
}
return result;
}
const arr = ['20','120','111','215','54','78'];
console.log(nextBiggest(arr));
回答by Denys Séguret
The simplest solution is to sort :
最简单的解决方案是排序:
// here's your array :
var stringArray = new Array('20','120','111','215','54','78');
// let's convert it to a real array of numbers, not of strings :
var intArray = stringArray.map(Number);
// now let's sort it and take the second element :
var second = intArray.sort(function(a,b){return b-a})[1];
If you don't want the simplest but the fastest (you probably don't need it), then you'd have to write your for
loop and store the two greatest elements while looping.
如果您不想要最简单但最快的(您可能不需要它),那么您必须编写for
循环并在循环时存储两个最大的元素。
回答by dan-lee
First sort it backwards and then get the second element:
首先向后排序,然后获取第二个元素:
['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
Obviously works with strings too.
显然也适用于字符串。
回答by Kevin Bowersox
Sort the array and then return the second index.
对数组进行排序,然后返回第二个索引。
var arr = ['20','120','111','215','54','78'];
arr.sort(function(a,b){
return b-a;
});
console.log(arr[1]);
回答by Mark Walters
Sort your array from smallest to largest, then grab second one from the end with .length-2
将您的数组从小到大排序,然后从末尾抓取第二个 .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;
回答by Moumita
You can try this:
你可以试试这个:
function second_highest(arr)
{
var second_highest = arr.sort(function(a, b) { return b - a; })[1];
return second_highest;
}