admin管理员组

文章数量:1302329

Say for example I have an array that looks like this:

var myArray = [[1,2,3],[1,2,3,4],[1,2],[1,2]];

I'm trying to find the first shortest array inside myArray which in this case would be myArray[2].

Obviously I could just write a loop, checking the length of each array and returning the smallest one. What I'm wondering is if there's a really clean or cleaver way to do it in javascript. Something along the lines of this: /

Thanks!

Say for example I have an array that looks like this:

var myArray = [[1,2,3],[1,2,3,4],[1,2],[1,2]];

I'm trying to find the first shortest array inside myArray which in this case would be myArray[2].

Obviously I could just write a loop, checking the length of each array and returning the smallest one. What I'm wondering is if there's a really clean or cleaver way to do it in javascript. Something along the lines of this: http://ejohn/blog/fast-javascript-maxmin/

Thanks!

Share Improve this question asked Aug 16, 2013 at 16:13 Ryan EppRyan Epp 9511 gold badge10 silver badges21 bronze badges 2
  • 3 You always will have to loop unless the outer array is sorted or something. Resig's implementation is not really faster (in terms of plexity), it only has shorter code than an explicit loop. – Bergi Commented Aug 16, 2013 at 16:16
  • No. But you can use panion hash tables and arrays to index and record size and order when you update it. Create a class that behaves like a list but implements a method that returns the most empty container. You can update this value when pushing something onto a container or popping something off. – Craig Commented Aug 16, 2013 at 16:17
Add a ment  | 

4 Answers 4

Reset to default 9

Well you could do it like this:

var shortest = myArray.reduce(function(p,c) {return p.length>c.length?c:p;},{length:Infinity});

This uses an internal loop so it's faster than manually running your own loop, but would require a shim to work in older browsers.

The way you are looking for using max or min looks like this.

Math.max.apply(Math, $.map(array, function (index) { return index.length }));

The trick is mapping to the inner arrays length attribute.

If by best you mean fastest time.. You will not achive a solution that is better than O(N), since you must check each element in the array (assuming it is unsorted).

Since you cannot achieve anything better than O(N), I see no reason not to do something like the following:

var myArray = [[1,2,3],[1,2,3,4],[1,2],[1,2]];

var shortestIndex = 0;

for( var i=1; i< myArray.length; i++){
    if(myArray[shortestIndex].length > myArray[i].length)
        shortestIndex = i;
}

now myArray[shortestIndex] is the shortest array.

Using javascript array reduce. Remember that a reducer will only return one value, we will be reducing the array into a value.

   reduce(callback,initialValue)

JavaScript invokes the callback function upon each item of the array

const findShortestArray = (arr = []) => {
  //  we iterate over array and "val" is the current array element that we are on
  // acc is the current result so far
  const res = arr.reduce((acc, val, index) => {
    if (!index || val.length < acc[0].length) {
      return [val];
    }
    if (val.length === acc[0].length) {
      acc.push(val);
    }
    return acc;
  // initial value=[]
  }, []);
  return res;
};

本文标签: javascriptWhat39s the best way to find the shortest array in a two dimensional arrayStack Overflow