freeCodeCamp Challenge Guide: Return Largest Numbers in Arrays

Return Largest Numbers in Arrays


Problem Explanation

You will get an array that contains sub arrays of numbers and you need to return an array with the largest number from each of the sub arrays.


Hints

Hint 1

You will get an array that contains sub arrays of numbers and you need to return an array with the largest number from each of the sub arrays. You will need to keep track of the array with the answer and the largest number of each sub-array.

Hint 2

You can work with multidimensional arrays by Array[Index][SubIndex]

Hint 3

Pay close attention to the timing of the storing of variables when working with loops


Solutions

Solution 1 (Click to Show/Hide)

(Procedural approach)

function largestOfFour(arr) {
  const results = [];
  for (let i = 0; i < arr.length; i++) {
    let largestNumber = arr[i][0];
    for (let j = 1; j < arr[i].length; j++) {
      if (arr[i][j] > largestNumber) {
        largestNumber = arr[i][j];
      }
    }
    results[i] = largestNumber;
  }

  return results;
}

Code Explanation

  • Create a variable to store the results as an array.
  • Create an outer loop to iterate through the outer array.
  • Create a second variable to hold the largest number and initialise it with the first number. This must be outside an inner loop so it won’t be reassigned until we find a larger number.
  • Create said inner loop to work with the sub-arrays.
  • Check if the element of the sub array is larger than the currently stored largest number. If so, then update the number in the variable.
  • After the inner loop, save the largest number in the corresponding position inside of the results array.
  • And finally return said array.

Relevant Links

Solution 2 (Click to Show/Hide)

(Declarative approach)

function largestOfFour(arr) {
  return arr.map(function(group) {
    return group.reduce(function(prev, current) {
      return current > prev ? current : prev;
    });
  });
}

Code Explanation

  • we map all items within the main array to a new array using Array.prototype.map() and return this array as the final result
  • within each inner array, we reduce its contents down to a single value using Array.prototype.reduce()
  • the callback function passed to the reduce method takes the previous value and the current value and compares the two values
  • if the current value is higher than the previous value we set it as the new previous value for comparison with the next item within the array or returns it to the map method callback if it’s the last item

Relevant Links

Solution 3 (Click to Show/Hide)

(Declarative approach)

function largestOfFour(arr) {
  return arr.map(Function.apply.bind(Math.max, null));
}

Code Explanation

TL;DR: We build a special callback function (using the Function.bind method), that works just like Math.max but also has Function.prototype.apply's ability to take arrays as its arguments.

  • We start by mapping through the elements inside the main array. Meaning each one of the inner arrays.
  • Now the need a callback function to find the max of each inner array provided by the map.

So we want to create a function that does the work of Math.max and accepts input as an array (which by it doesn’t by default).

In other words, it would be really nice and simple if this worked by itself:

Math.max([9, 43, 20, 6]); // Resulting in 43

Alas, it doesn’t.

  • To do the work of accepting arguments in the shape of an array, there is this Function.prototype.apply method, but it complicates things a bit by invoking the context function.

i.e. Math.max.apply(null, [9, 43, 20, 6]); would invoke something like a Max.max method. What we’re looking for… almost.

Here we’re passing null as the context of the Function.prototype.apply method as Math.max doesn’t need any context.

  • Since arr.map expects a callback function, not just an expression, we create a function out of the previous expression by using the Function.bind method.
  • Since, Function.prototype.apply is a static method of the same Function object, we can call Function.prototype.bind on Function.prototype.apply i.e. Function.prototype.apply.bind.
  • Now we pass the context for the Function.prototype.apply.bind call (in this case we want Math.maxso we can gain its functionality).
  • Since the embedded Function.prototype.apply method will also require a context as it’s 1st argument, we need to pass it a bogus context.
    • So, we pass null as the 2nd param to Function.prototype.apply.bind which gives a context to the Math.max method.

    • Since, Math.max is independent of any context, hence, it ignores the bogus context given by Function.prototype.apply method call.

    • Thus, our Function.prototype.apply.bind(Math.max, null) makes a new function accepting the arr.map values i.e. the inner arrays.

Relevant Links

Solution 4 (Click to Show/Hide)

(Recursive approach)

function largestOfFour(arr, finalArr = []) {
  return !arr.length
    ? finalArr
    : largestOfFour(arr.slice(1), finalArr.concat(Math.max(...arr[0])))
}
203 Likes

Superb explanation. Thank you!

7 Likes

I just want to say: these algorithm help solutions are AMAZING. I am generally able to hack together something for a solution for each of the challenges but your intermediate/advanced solutions are blowing my mind. So much to learn! Thank you for doing these!

62 Likes

Thanks, there have been on the wiki long before the forum came and the solutions are from different campers, I did provide at least one solution for most of them but feel free to contribute by creating or editing existing wiki articles.

8 Likes

Well I’m not that familiar but it seems that the bind was to apply the function instead of making your own. As to why would you want to use a built in function instead of making your own that uses it, I think is like avoiding the middleman.

1 Like

Benchmark by @P1xt
Results … ES6 and “Function.apply.bind” may look all snazzy and smart but neither is the actual advanced solution. Because, an advanced solution takes efficiency into account and the “vanilla” solution is by far the fastest / most efficient of the three.

  1 var Benchmark = require('benchmark');
  2 var suite = new Benchmark.Suite;
  3
  4 function largestOfFourBind(arr) {
  5   return arr.map(Function.apply.bind(Math.max, null));
  6 }
  7
  8 function largestOfFourES6(arr) {
  9   return arr.map((a) => Math.max(...a));
 10 }
 11
 12 function largestOfFourVanilla(arr) {
 13   return arr.map(function(a) {
 14     return Math.max.apply(null, a);
 15   });
 16 }
 17
 18 // add tests
 19 suite.add('bind', function() {
 20     largestOfFourBind([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [10    00, 1001, 857, 1]]);
 21 })
 22 .add('es6', function() {
 23     largestOfFourES6([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100    0, 1001, 857, 1]]);
 24 })
 25 .add('vanilla', function() {
 26     largestOfFourVanilla([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39],     [1000, 1001, 857, 1]]);
 27 })
 28 // add listeners
 29 .on('cycle', function(event) {
 30    console.log(String(event.target));
 31 })
 32 .on('complete', function() {
 33    console.log('Fastest is ' + this.filter('fastest').map('name'));
 34 })
 35 // run async
 36 .run({ 'async': true });

output:

bind x 355,997 ops/sec ±4.59% (71 runs sampled)
es6 x 91,822 ops/sec ±1.26% (75 runs sampled)
vanilla x 802,980 ops/sec ±1.60% (76 runs sampled)
Fastest is vanilla

EDIT

As a thought experiment I went back and tested the beginner and intermediate solutions against the other three.

The beginner solution was actually the fastest far and away by a LARGE margin.

beginner x 3,482,777 ops/sec ±1.66% (69 runs sampled)
intermediate x 497,563 ops/sec ±1.83% (71 runs sampled)
bind x 378,711 ops/sec ±1.62% (74 runs sampled)
es6 x 95,515 ops/sec ±1.03% (84 runs sampled)
vanilla x 815,023 ops/sec ±0.97% (81 runs sampled)
Fastest is beginner

This was an important lesson, to me at least, that the solution that uses the least characters, or that uses the most advanced features of the language, isn’t necessarily the most advanced solution if the beginner solution is more efficient.

59 Likes

Here is a solution I came up with utilising the Math object.

function largestOfFour(arr) {  
  var maxNumbers = [];
  
  for (var i = 0; i < arr.length; i++) {
    maxNumbers.push(Math.max.apply(null, arr[i]));
  }
  
  return maxNumbers;
}
49 Likes

Hey Rafase,
love the solutions and they have been very helpful when I get stuck.

2 Likes

i was thinking this but i guess I never learnt it yet

3 Likes

In general, loops are always faster than function calls. Engines and compilers are highly optimized for loops. Function calls involve context switching which may bring measurable impact on performance.

8 Likes

Hi everyone,

I came up with a the following solution:

function largestOfFour(arr) {

  var arrNew = [];

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

  return arrNew;

 } 

Code Explanation:

  • Create an empty array as variable in which you store the results
  • Create a for loop to iterate through arr
  • Use the push() method to store the largest number as item in the array
  • Inside the push() method use arr[i] as a parameter so that everytime the the loop iterates over an outer array element it will include the sort() method
  • Use the sort() method to the sub array from largest to smallest (it is the same code as explained in the lesson Sort Arrays with .sort from the course Object oriented and functional programming)
  • After the you close .sort() add [0] so the sub array is stored in your empty array when you use .push() (as explained in hint #2)
  • Close the for loop
  • Return the formerly empty array which now contains the largest numbers of arr

What do you guys think?
English is not my native language so there might be some mistakes or the explanation is not quite satisfying.

Therefore I would be glad about any kind of feedback

32 Likes

Here’s what I came up with:

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

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
17 Likes

I have used two ways to solved this problem, 1) by map and other 2) by loop.
I have gone through some of the answers, and I found most of them complicated. Not complicated but used so many things. So this is the neatness I come up with.

function largestOfFour(arr) {
var array = arr.map(function(large){
return Math.max.apply(null, large);
});
return array;
}
OR
function largestOfFour(arr) {
var arr1=[];
for(i=0; i<arr.length;i++){
arr1.push(Math.max.apply(null, arr[i]));
}
return arr1;
}

5 Likes

Using a mix of the procedural and advanced methods
function largestOfFour(arr) {
var arrNew = arr.map(function(x){
var max = 0;
for (var i = 0; i < x.length; i++){
if (x[i] > max){
max = x[i];
}
}
return max;
});
return arrNew;
}

1 Like

Working on the second declarative solution. If I copy and paste it I get
group.reduce is not a function

I’ve tried in Node and in the browser. What am I doing wrong?

1 Like

My solution :slight_smile:

function largestOfFour(arr) {
  var returned = [];
  var shift = [];
  

function lowHigh(a, b) {
	return a - b;
}


  for (var i = 0; i < arr.length; i++){
    for(var j = 0; j < arr[i].length; j++){
     
    arr[i].sort(lowHigh);
    arr[i].reverse();
    
    }
    returned.push(arr[i][0]);
    
  }
  return returned;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
2 Likes

Hi there,

I love the explanations of different solutions for these algorithm assignments but I had a quick question regarding the solution of the first one.

When iterating over the second for loop, why do we initialize sb = 1 instead of 0?

Thanks!

4 Likes

Hello countercoded

I’m having the same question: Short Anwser or How I understand it:

it allows the " for loop " to do the first iteration.

The case arr[n][0] is already inside the first iteration of the loop by largestNumber. So, on the first loop iteration, you are comparing case 1 (arr00) against case 2 (arr01) , otherwise, the first iteration of the loop would be comparing case 1 against case 1.

As you can see largestNumber is outside the loop:

var largestNumber = arr[n][0];

Try to visualize what would happen is sb is set up to 0.

for (var sb = 1; sb < arr[n].length; sb++) {
      if (arr[n][sb] > largestNumber) {
        largestNumber = arr[n][sb];

Well, at least is how i see it.
Regards,
Martin

Note: I’m not an Expert, this is just an opinion.

2 Likes