# freeCodeCamp Challenge Guide: Return Largest Numbers in Arrays

After going through the Basic Algorithm, I’m definitely noticing I’ll need to start looking at other sources along with FCC to progress. A lot of the solutions needed are using functions and elements that weren’t taught in the Front End Dev Cert section.

So much to learn, so much to figure out!

5 Likes

Yeah that’s true and basically what Quincy Larsson (the creator of FCC) says.
I would suggest to look into the “get job ready course”.

1 Like

Here is what I have come up with: (I’m just over a week into coding)

function largestOfFour(arr) {
// You can do this!
var sorted = [];
var largest = [];

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

for (var i=0; i < arr.length; i++) {
sorted.push(arr[i].sort(sortNumber));

}
for (var j=0; j < sorted.length; j++) {
largest.push(sorted[j].pop());
}

return largest;

}

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

3 Likes

And here I go:

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

//array for storing largest numbers
var largestArr = [];
var largest = 0;//create largest number to have something to compare to
//Loop through an array
for(var i=0; i< arr.length; i++){
//loop through an indicidual numbers of each array
for(var y=0; y< arr[i].length; y++){
//compare each number to our largest number declared earlier
if(arr[i][y] > largest){
largest = arr[i][y];
}

}

largestArr.push(largest);//add largest number to an array
largest = 0;//reset largest number for the next loop phase
}
return largestArr;
}

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

I don’t know about you guys, but those solutions in spoiler section are a bit complicate. Maybe bc I’m new to programming. Anyway after a couple of hours ;-), with breaks I came with my own algorithm:

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

var newArr = [];
for(var i = 0; i < arr.length; i++){

newArr[i] = Math.max.apply(null, arr[i]);

}

return newArr;
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);``````
5 Likes

I did it with different way, Can anyone comment my mistakes;

``````    function largestOfFour(arr) {
// You can do this!
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
arr1.push(arr[i].sort(function(x,y) {return y - x;}));
}
arr1 = [arr1[0][0],arr1[1][0],arr1[2][0],arr1[3][0]];
// console.log(arr1);
return arr1;
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
``````

`

1 Like

My solution:

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

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

Up to this point I’ve been struggling with coming up with some “slick” code compared to the hints.But in this case I think I’ve leaped ahead. This compact solution will work for any number of arrays and any number values in a sub-array:

``````
function largestOfFour(arr) {
if (Array.isArray(arr)) {
subArray = [];
for (i = 0; i < arr.length; i++) {
subArray.push(Math.max.apply(null, arr[i])) ;
}
return subArray;
}
return "Not an Array";
}

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

Here is the solution that I came up with:

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

``````}
result.push(max);
max = 0;
``````

}
return result;
}

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

1 Like

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

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

1 Like

I agree with you Naib…I came up with the same solution.

3 Likes

Hello,

I am wondering what is wrong with this code, it does not pass on the following array of arrays:

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

My code is following:

Code

Brg

1 Like

I’ve seen a lot of similar solutions but I thought I’d post mine for comparison:
``` function largestOfFour(array) { var arr = []; //sub-array var ans = [];// array of answers for(i = 0; i < array.length; ++i) { arr = array[i]; /.push adds data to end of ans, .sort sorts the data(small - big) then gives the last element (the biggest number)./ ans.push(arr.sort(function(b, a){return a < b;})[3]); // } return ans; }```

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

1 Like

function largestOfFour(arr) {
// You can do this!
var newArray = [];
for(var i=0;i<arr.length;i++){
for(var y=0;y<arr[i].length;y++){
if(newArray[i]==undefined){
newArray[i] = arr[i][y];
} else if(newArray[i]<arr[i][y]){
newArray[i] = arr[i][y];
}
}
}
return newArray;
}

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

1 Like

Seem to have found a quick elegant way - found something similar to the basic solution and when trying to recreate the intermediate solution this seemed to work for everything:

function largestofFour(arr) {
arr.reduce(function(a,b) {
return b-a;
});
}

1 Like

Came up with the exact same solution

1 Like

I used sort. Here is my solution.
``` function largestOfFour(arr) { var newArray = []; for (var i = 0; i < arr.length; i++) { arr[i].sort(function(a, b){ return b - a; }); newArray.push(arr[i][0]); } return newArray; } ```
Is that acceptable? I’ve only been learning for a week, so the intermediate and advanced solutions are tough for me to decipher, while the beginner solution seems overly complicated.

3 Likes

This was my version of sorting through the array then pushed the values into a new array

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

var nArray=[];

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

}
}

nArray.push(arr[0][0],arr[1][0],arr[2][0],arr[3][0]);

return nArray;
}

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

Here’s my solution with annotation

it is very basic.

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

//Combined Array to store result of each largest subarray
let combinedArr = [];

//Iterate array + child arrays
for (let i=0; i<arr.length;  i++){

//Benchmark / reset for subarray compare
let curMax = 0;

//Compare subarray for max value
for (let j=0; j<arr[i].length; j++){
if (curMax <= arr[i][j]){
curMax = arr[i][j];
}
}

//Store largest number in new array current index
combinedArr[i]=curMax;
}
return combinedArr;
}

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

I don’t know what’s strange but I think my solution is too small compared with your solutions guys !! if there is anything wrong, please tell me.
here is it

function largestOfFour(arr) {
var result = [];

for (var x = 0 ; x < arr.length ; x++){
arr[x].sort(function(a,b){return a-b;});
result[x] = arr[x].pop();
}
return result;
}

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

1 Like