Skip to content Skip to sidebar Skip to footer

Finding The Highest Number Of Elements In Arrays

I want to find the longest array and get his length. This is array: [['667653905', 'Johjn', 'Smith'], ['500500500', 'John', 'Smith2', 'Another field'], ['12342312', 'asd', 'asda

Solution 1:

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.

Solution 2:

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:

var max = 0;
var explode = newArray();

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

Post a Comment for "Finding The Highest Number Of Elements In Arrays"