Find the index of the longest array in an array of arrays

If you have an array containing an undetermined number of arrays

Example:

var masterArray = [ [1,2,3,4,5], [1,2], [1,1,1,1,2,2,2,2,4,4], [1,2,3,4,5] ]; 

What is the efficient way to find the index of the longest array in masterArray? (in this example, the index will be 2).

+15
source share
12 answers
 var masterArray = [ [1,2,3,4,5], [1,2], [1,1,1,1,2,2,2,2,4,4], [1,2,3,4,5] ]; 

Single line:

 masterArray.map(function(a){return a.length;}).indexOf(Math.max.apply(Math, masterArray.map(function(a){return a.length;}))); 

But it is better to cache map results.

 var lengths = masterArray.map(function(a){return a.length;}); lengths.indexOf(Math.max.apply(Math, lengths)); 

Note that even this code indexOf over the array 3 times ( map , max , indexOf separately).
For more efficient use, you should use a manual iterative array.

 var max = -Infinity; var index = -1; masterArray.forEach(function(a, i){ if (a.length>max) { max = a.length; index = i; } }); 

Reduce Method:

 masterArray.reduce(function(maxI,el,i,arr) {return el.length>arr[maxI].length ? i : maxI;}, 0) 
+13
source

.reduce - .reduce way to do this:

 masterArray.reduce(function (pending, cur, index, ar) { ar[ pending ].length > cur.length ? pending : index }, 0); 

Or with ES6:

 masterArray.reduce((p, c, i, a) => a[p].length > c.length ? p : i, 0); 
+9
source

 masterArray.reduce(function(a,i,ii){ if (ii === 1){ return a }; if (i.length > a.length){ return i } return a }) 
+4
source

The reducer iterates through an array of arrays, where the accumulator represents the index of the longest array, starting at index 0 .

For each iteration, the current length of the element (which is an array) is compared with the length of the array by index (accumulator) from the list of arrays, and if it is larger, the accumulator increases.

 var arrays = [ [1,1,1,1,1], [1,1], [1,1,1,1,1,1,1,1,1,1], // โฌ… The longest, which is at index 2 [1,1,1,1] ] var indexOfLongestArray = arrays.reduce((idx, arr) => arr.length > arrays[idx].length ? idx + 1 : idx , 0) // print result: console.log( indexOfLongestArray ) 

+3
source

Lazy UnderscoreJS:

 _.max(masterArray, function(i){ return i.length; }) 
+1
source

Sort the list of indexes by length in descending order and take the first one:

 a.map((e, i) => i) . sort((i, j) => a[j].length - a[i].length) [0] 
+1
source

If you use Lodash (since version 4.0) , you can easily use _.maxBy and _.size as iteratee:

 _.maxBy(masterArray, _.size) -> [1, 1, 1, 1, 2, 2, 2, 2, 4, 4] 

To find minimal use of _.minBy

 _.minBy(masterArray, _.size) -> [1, 2] 
+1
source

I ran into this today and found this question.

Here is a more modern approach:

 const longestArray = masterArray.reduce((acc, curr, index) => curr.length > acc.length ? index : index - 1 ); 
  • Now the code finds the index of the longest array, not the longest array (sorry, I read the question incorrectly).
+1
source

You can iterate over all the elements of an external array using the for loop and compare the length of each of your elements with the longest array you have found so far.

The following function returns the index of the longest array, or -1 if the array is empty.

 function indexOfLongest(arrays) { var longest = -1; for (var i = 0; i < arrays.length; i++) { if (longest == -1 || arrays[i].length > arrays[longest].length) { longest = i; } } return longest; } var masterArray = [ [1,2,3,4,5], [1,2], [1,1,1,1,2,2,2,2,4,4], [1,2,3,4,5] ]; document.write(indexOfLongest(masterArray)); 
0
source

Try using a while

 var masterArray = [ [1, 2, 3, 4, 5], [1, 2], [1, 1, 1, 1, 2, 2, 2, 2, 4, 4], [1, 2, 3, 4, 5] ]; var i = 0, len = masterArray.length; while (i < len) { // if array[i + 1] exists // and array[i + 1] length greater than array[i] length // and i + 1 equals array length - 1 // break if (masterArray[i + 1] && masterArray[i + 1].length < masterArray[i].length && i + 1 === len - 1) { break } // else increment i else { ++i } } console.log(masterArray[i]) 
0
source

Using lodash:

 _.max(_.map(masterArray, function(v, k) { return { id: k, size: v.length }; }),'size').id; 

This creates a new array with objects having "id" and "size", then finds the maximum size in this array and returns its "id".

jsfiddle: https://jsfiddle.net/mckinleymedia/8xo5ywbc/

0
source

if the order of the elements in the array does not matter to you , and you just want to use the longest array, you can sort the array by length

  masterArray.sort((a, b) => b.length - a.length); const theLongest = masterArray[0]; 

in this case, the first element will always be the longest

0
source

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


All Articles