查找数组中元素数量最多

时间:2022-08-22 14:07:44

I want to find the longest array and get his length.

我想找到最长的阵列并获得他的长度。

This is array:

这是数组:

 [["667653905", "Johjn", "Smith"], ["500500500", "John", "Smith2", "Another field"], ["12342312", "asd", "asdasd", "fghfgh", "fghfgh"]]

Currently i am using loop but this is very slow solution if i have 50k arrays:

目前我正在使用循环,但如果我有50k数组这是非常慢的解决方案:

for(var i = 0; i<explode.length; i++) {
        console.log(explode[i].length);
    }

3 个解决方案

#1


4  

You can find the longest array with .reduce()

您可以使用.reduce()找到最长的数组

var longest = theArray.reduce(function(lsf, a) {
    return !lsf || a.length > lsf.length ? a : lsf;
}, null);
console.log("longest array has " + longest.length + " elements");

#2


3  

Just a complement to the answer above. If the array is 50K or more, it may be better to consider moving the code into the backend firstly. In that case, it could be done by java or other low level languages, or even using parallel programming solution. The speed will be increased dramatically.

只是上面答案的补充。如果数组是50K或更多,最好考虑首先将代码移入后端。在这种情况下,它可以通过java或其他低级语言完成,甚至可以使用并行编程解决方案。速度将大大提高。

#3


0  

You could only make an assignment (or output) fewer times, to boost it up.

您只能进行较少次的分配(或输出),以提升它。

Although I'm not a hundred percent sure the if () would actually be faster than other aproach (as the use of reduce() [don't know it's implementation]), try this implementation with the 50k array:

虽然我不是百分百肯定if()实际上会比其他aproach更快(因为使用reduce()[不知道它的实现]),尝试使用50k数组的这个实现:

var max = 0;
var explode = new Array();

explode = [["667653905", "Johjn", "Smith"],
 ["500500500", "John", "Smith2", "Another field"], 
 ["12342312", "asd", "asdasd", "fghfgh", "fghfgh"]];

var explodeSize = explode.length;
for (var i = 0; i<explodeSize; i++) {
    if (max < explode[i].length) {
        max = explode[i].length;
    }
}
console.log(max);

#1


4  

You can find the longest array with .reduce()

您可以使用.reduce()找到最长的数组

var longest = theArray.reduce(function(lsf, a) {
    return !lsf || a.length > lsf.length ? a : lsf;
}, null);
console.log("longest array has " + longest.length + " elements");

#2


3  

Just a complement to the answer above. If the array is 50K or more, it may be better to consider moving the code into the backend firstly. In that case, it could be done by java or other low level languages, or even using parallel programming solution. The speed will be increased dramatically.

只是上面答案的补充。如果数组是50K或更多,最好考虑首先将代码移入后端。在这种情况下,它可以通过java或其他低级语言完成,甚至可以使用并行编程解决方案。速度将大大提高。

#3


0  

You could only make an assignment (or output) fewer times, to boost it up.

您只能进行较少次的分配(或输出),以提升它。

Although I'm not a hundred percent sure the if () would actually be faster than other aproach (as the use of reduce() [don't know it's implementation]), try this implementation with the 50k array:

虽然我不是百分百肯定if()实际上会比其他aproach更快(因为使用reduce()[不知道它的实现]),尝试使用50k数组的这个实现:

var max = 0;
var explode = new Array();

explode = [["667653905", "Johjn", "Smith"],
 ["500500500", "John", "Smith2", "Another field"], 
 ["12342312", "asd", "asdasd", "fghfgh", "fghfgh"]];

var explodeSize = explode.length;
for (var i = 0; i<explodeSize; i++) {
    if (max < explode[i].length) {
        max = explode[i].length;
    }
}
console.log(max);