# Return Largest Numbers in Arrays

Learn how to solve the freeCodeCamp algorithm 'Return Largest Numbers in Arrays' using a for-loop and the Array.push() method.

In this freeCodeCamp algorithm we need to return the largest numbers of each given array.

## Spec

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

## Requirements

• `largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])` should return an array.
• `largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])` should return `[27, 5, 39, 1001]`
• `largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])` should return `[9, 35, 97, 1000000]`
• `largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])` should return `[25, 48, 21, -3]`

## Psuedocode

``````// Create a function that accepts an array of sub-arrays
// Create a new array to store the largest numbers from each sub-array
// Loop over each sub-array
// Sort the numbers from high to low with the given sub-array
// Push the largest number of sub-array to the array of largest numbers
// Return largest numbers array
``````

## Solving the Algorithm

### Setting up our function

Let's create our function that will accept an array of sub-arrays. Don't get too hung up on this as it doesn't need to be anything more than a variable. Just assume that this variable is going to be an array of sub-arrays.

``````function largestOfFour(arr) {

}
``````

### Create an array to store the largest number of each sub-array

This is pretty straight forward. We're going to setup an array that we can use to store the largest numbers from each sub-array.

``````function largestOfFour(arr) {
var largestNums = [];
}
``````

### Loop over each sub-array

``````function largestOfFour(arr) {
var largestNums = [];

for(var i in arr) {
console.log(arr[i])
}

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

// Returns each sub-array
// [4, 5, 1, 3]
// [13, 27, 18, 26]
// [32, 35, 37, 39]
// [1000, 1001, 857, 1]

``````

Test this code in your browser console

### Sort numbers in each sub-array from high to low

JavaScript has a handy method we can use called `Array.sort()` that will, hence the name, allow us to sort an array.

[By default] all non-undefined array elements are sorted by converting them to strings and comparing [the] strings in Unicode code point order. For example, "Banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order. All undefined elements are sorted to the end of the array.

Note: If a `compareFunction` is supplied, than the sort order can be altered.

If `compareFunction` is supplied, all non-undefined array elements are sorted according to the return value of the compare function...

In our case we want to sort high to low, so within our `.sort()` method, we can pass in two arguments, `a` and `b` (though, they could be named anything). Then instead of sorting them by the default of low to high, we will instead need to reverse the order by returning `b - a`.

This might be confusing at first, so I would definitely recommend reading through the Mozilla Docs as much as possible and playing around with the example code on their site to gain a more thorough understanding.

Let's give this a shot:

``````function largestOfFour(arr) {

var largestNums = [];

for(var i in arr) {
arr[i].sort((a, b) => {
return b-a;

});
console.log(arr[i]);
}
}

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

// Returns each sub-array sorted high to low
// [5, 4, 3, 1]
// [27, 26, 18, 13]
// [39, 37, 35, 32]
// [1001, 1000, 857, 1]

``````

Test this code in your browser console

CHALLENGE: With what you now know about `.sort()`, how would you sort the sub-arrays from low to high?

### Push the largest number of sub-array to the array of largest numbers

Now that we have the sub-arrays sorted in a nice, orderly fashion, let's take the highest number from each sub-array and add it to the `largestNums` array that we created earlier.

We do know that with sorting from high to low, index `0` of each sub-array will always hold the highest number.

Let's use the handy Array.push() method that we know and love.

``````function largestOfFour(arr) {

var largestNums = [];

for(var i in arr) {
arr[i].sort((a, b) => {
return b-a;
});
largestNums.push(arr[i][0]);
}
}

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

``````

### Return the array with the largest numbers from each sub-array

Last but not least, we must return the `largestNums` array.

``````function largestOfFour(arr) {

var largestNums = [];

for(var i in arr) {
arr[i].sort((a, b) => {
return b-a;
console.log(arr[i])
});

largestNums.push(arr[i][0]);
}
return largestNums;
}

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

// Returns: [5, 27, 39, 1001]

``````

## Final Thoughts

This freeCodeCamp algorithm was definitely pretty interesting and it introduced us to the useful `.sort()` method.

Hopefully you found this to be a helpful walkthrough on how to return the largest numbers in arrays.

Shoot me an email at `[email protected]` with any questions and if you enjoyed this, stay tuned and subscribe below! ðŸ‘‡

Help us improve our content