[challenge] Return Largest Numbers in Arrays

I read an article on Medium regarding how to find largest numbers in arrays. There are 3 approaches explained in the article- with a For loop; using the reduce() method & using Math.max().
Below is how the author writes the code using the For loop:

function largestOfFour(arr) {
  var largestNumber = [0,0,0,0];
  for(var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++){
   for(var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++){
     if(arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]){
        largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
      }
   }
}
return largestNumber;
}

And I used For loop too, but my code is a bit different. Mine is obviously longer and seems laggard. What part can be improved? Can someone please help me? Thanks! :slight_smile:
Below is my solution:

function largestOfFour(arr) {
  
  
  for(var i = 0; i < arr.length; i++){
   arr[i].sort(function(a, b){
      return b - a;
    });
   
  }
   var largestArray = [];
   for(i =0; i < arr.length; i++){
     largestArray.push(arr[i][0]);
   }
  
  return largestArray;
}

I think your code is fine.

But if you want improvements, you could get rid of the second for loop:

function largestOfFour(arr) {
  var largestArray = [];
  
  for(var i = 0; i < arr.length; i++){
   arr[i].sort(function(a, b){
      return b - a;
    });
    largestArray.push(arr[i][0]);
  }
   
  return largestArray;
}

Also you could use ..map() and get rid of for loop altogether (map is just a fancy for loop :slight_smile: )

function largestOfFour(arr) {
  return arr.map(ar => {
    ar.sort((a, b) => b - a)
    return ar[0]
  })
}

Thanks! It really helps!

Thanks for the suggestion!
But I don’t understand arr.map(ar => … what is “=>” used for?

It’s a new way of writing function introduced in latest version of JavaScript (ES6). You’ll see it a lot. It’s called “fat arrow”.

map(ar => {...

is the same as

map(function(ar) {...

For a challenge solution, your code is absolutely fine and does everything it should do.

Regarding it being longer, do you mean lines of code or execution time? If it’s lines of code, you shouldn’t worry about trying to condense everything in as tight as possible. It’s much more important that your code is readable than packed in as tight as possible.

Regarding execution time, your code “theoretically” will take longer to execute than the solution you found. This is because you use the array sort() method. What you are doing in your solution is actually sorting every array, which takes longer than just looking for the largest item. If you think about it, a sort algorithm needs to find the biggest value and put it first. But then it needs to find the “second” biggest item and place it second, and again for the third, and so on and so on. The problem only wants the biggest from an array, so a lot of extra work is being done by sort() that doesn’t need to be. Practically, it’s probably not a big deal because you’ll only start to see issues when you start scaling the data (think an array with a billion elements). In computer science terms, the run-time complexity of your solution is most likely O(n log n) since it is based on sort(), while the run-time complexity of the solution you found is only O(n) (I am deliberately assuming that the number of arrays being sorted is insignificant.)

If this were production code, the one point I’d make about your solution is that it changes the arrays passed in to it, which may be ok, or may not be. The sort() method sorts an array in place, effectively changing it for the caller. This may be ok or not depending on who is using it. A comment mentioning this would be worthwhile.

But as I said at the beginning, overall your solution is a fine one and a correct one.

References:

Curious what I could improve in my solution?


function largestOfFour(arr) {
  var x = [];
  for (var j = 0; j < arr.length; j++){
    x.push(0);
  for (var i = 0; i < arr.length; i++){
      if (arr[j][i]>x[j]){
      x[j] = arr[j][i];
    } 
    
  } 
  
}
  return x;
}

largestOfFour([[4, 11, 7, 9], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

thanks

Your solution doesn’t work in all cases actually.

My first recommendation is a minor one but surprising helpful and important, make sure you are indenting consistently and correctly. Your second for loop should be indented relative to the first.

My second recommendation is to try a different test case and see how it fails.

largestOfFour( [4, 11], [7, 9, 13, 27, 18, 29], [0], [50, 45] );
1 Like

Thank you for taking the time to look at my code and feedback to me- much appreciated

I thought about it for a bit before it dawned on me.
I was not iterating through the full sub array!

function largestOfFour(arr) {
  // You can do this!
  var x = [];
  for (var j = 0; j < arr.length; j++){
    x.push(0);
    
    for (var i = 0; i < arr[j].length; i++){
      if (arr[j][i]>x[j]){
      x[j] = arr[j][i];
    } 
    
  } 
  
}
  return x;
}

largestOfFour([[4, 11], [7, 9, 13, 27, 18, 29], [0], [50, 45]]);