Return largest numbers in arrays

Hello folk,
Here is my solution. It works, but If you have something more simple, please show me, so i could improve mine. I think we could do this with comparison operators…but i didn’t try…

function largestOfFour(arr) {
// You can do this!
arr.sort(function(a,b){
return (a-b);});
result=[];
for (i=0;i<arr.length;i++){
for(j=0;j<arr[i].length;j++){
arr[i].sort(function(a,b){
return (b-a);});
}
}
for (k=0;k<arr.length;k++){
result.push(arr[k][0]);
}

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

Hello ! Let me comment the code and then propose my solution

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

This doesn’t make much sense right now. Look at the argument you are given : an array containing a bunch of number arrays.

when calling arr.sort this way, you are trying to sort the containers but by using “a - b” you are working with numbers. This function fails because you are working with the arrays and not the numbers inside.

To fix this, you need to iterate through the arrays and work with the actual numbers. This can be done either with a for loop or Array.prototype.forEach

for (i=0;i for(j=0;j arr[i].sort(function(a,b){

This line just doesn’t work. You can’t embed another for loop this way. Also notice that you aren’t incrementing “i” anywhere, so this just won’t go. The correct syntax for a for loop is :

for (var i = 0; i < arr.length; i++) {
  // magic happens here
}

On a sidenote, you have to ask yourself if something’s wrong with this : You call .sort two times, and in the end you are reversing the return result.

Arr only needs to be sorted once.

SPOILER ALERT
Here is my solution :

function largestOfFour(arr) {

  var sorted = [];

  arr.forEach( function( item ) {
    sorted.push( item.sort( function( a,b ) { return b > a; } ) [0] );
  });

  return sorted;
}

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

arr.forEach permits me to work with each array inside, as “item”

item.sort will sort each array from the highest to lowest ( b > a )

We then push the index 0, which is the highest in the array sorted.

Good luck !

5 Likes

Thank Mizu,
i just noticed that the interpreter didn’t set my code correctly when i pasted it. But in fact, mine works. Thanks for your brilliant solution. Please check this, i thinks the interpreter set it correctly this time.

function largestOfFour(arr) {
// You can do this!
arr.sort(function(a,b){
return (a-b);});
result = [];
for (i=0; i < arr.length; i++){
for(j=0;j < arr[i].length; j++){
arr[i].sort(function(a,b){
return (b-a);});
}
}
for (k=0; k < arr.length; k++){
result.push(arr[k][0]);
}

return result.reverse();
}

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

1 Like

Much better, yeah it seemed some little things were missing. Althought your code works, there are uneccessay bits that can be removed (in bold) :

function largestOfFour(arr) {

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

return result .reverse();
}

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

1 Like

yes, indeed, you are right, my first sorting is useless :+1:

1 Like

function largestOfFour(arr) {

// I create an empty array
result=[];
// i sort the arr arrays
for (i=0; i < arr.length;i++){
for(j=0; j< arr[i].length;j++){
arr[i].sort(function(a,b){
return (b-a);});
}
}
// i push all the first items of each array in result
for (k=0;k < arr.length;k++){
result.push(arr[k][0]);
}
// i return result
return result;
}

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

1 Like

BTW, just wanted to mention that when you type in code, there is a button available on the editor to format it so that it looks nice. Paste in your indented code, select it, then press the “</>” button and it will be much more readable.

2 Likes

thanks so much :smiley:

This was my solution.

I basically iterate through the array, sorted each element and popped off the largest number to a new array.

Let me know what you all think.

Thank you,

Allan


function largestOfFour(arr) {

var arrToReturn = [];

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

return arrToReturn;
}

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

My solution:

function largestOfFour(arr) {
// You can do this!
var a = arr[0];
var b = arr[1];
var c =arr[2];
var d = arr[3];
var k = [Math.max.apply(Math,a),Math.max.apply(Math,b),Math.max.apply(Math,c),Math.max.apply(Math,d)];

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

You really only need one For-Loop to do the sort, push and shift.

function largestOfFour(arr) {
	var newArray = [];
  	for (var i=0; i<arr.length; i++) {
  		arr[i].sort(function(x, y) {
  			return y - x;
  		});
  	newArray.push(arr[i].shift());	
  	}
 return newArray;
}
2 Likes

without sort

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

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

I made this way and it worked:

function largestOfFour(arr) {
  // You can do this!
  for (i = 0; i < arr.length; i++) {
    arr[i] = Math.max.apply(null, arr[i]);
  }
  return arr;
}

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

How about This?

function largestOfFour(arr) {

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

  newArray.push(arr[i].sort(function(a,b){return b-a;}));

}
var sortNumber = [];
sortNumber.push(arr[0][0],arr[1][0],arr[2][0],arr[3][0]);
return sortNumber;
}

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

This was the solution I settle on.

Spoiler
function largestOfFour( arr ){
  return arr.map( ( a ) => Math.max.apply( undefined, a ) );
}

I came up with this. Although now as I look through other people’s solutions I should practice using libraries :slight_smile:

function largestOfFour(arr) {
  // You can do this!
  var newArr = [];
  var max = 0;
  
  for(var i = 0; i < 4; i++){
    max = 0; //reset max through each iteration
    for(var j = 0; j < 4; j++){
      if(arr[i][j] > max){
        max = arr[i][j]; //update max
        newArr[i] = max; //put max in array
      }
    }
  }  
  return newArr;
}

Also, if you guys didn’t know how to format your code when you copy+paste it, read this

Hi @suda86

function largestOfFour(arr) {

  var newArr = [];
  var maxiNum = 0;
  for(i=0; i< arr.length; i++){
  for(j=0; j<arr[i].length; j++){
  if(arr[i][j] > maxiNum){
    maxiNum = arr[i][j];
        }
}
newArr.push(maxiNum);
**maxiNum =0; //Why do we have to put this here???**
}
return newArr;
}

maxiNum =0; //Why do we have maxiNum = 0 here? What’s the purpose.

Thanks.

Hey all. I know that there are often multiple ways of going about solving these challenges, so I figured I’d zip over and compare my solution to others’. It’s interesting to see how other people think. Below is my solution, which seems a bit overly complicated, but anyone have any comments on the way I went about it? Thanks :slight_smile:

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

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

I just finished the algorithm challenges and was checking the forum (and that’s why I’m here). Here is how I solved it:

[code]function largestOfFour(arr) {
// You can do this!
return arr.map(function(val){
return val.reduce(function(x,y){
return x>y?x:y;
});
});
}

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

2 Likes

Hi there,

I know this is quite a stretch but I just came across your solution and like how elegant it looks.

Are you able to assist me in breaking down the code starting from calling the map() method followed by (a) and the arrow function? I’m still very new to programming so I don’t know the syntax very well which is why I can’t really follow your logic.

Thanks to anyone who can help!