Return largest numbers in arrays

Return largest numbers in arrays
0

#4

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]]);


#5

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


#6

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]]);


#7

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.


#8

thanks so much :smiley:


#9

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]]);


#10

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]]);


#11

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;
}

#12

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]]);


#13

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]]);

#14

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]])


#16

This was the solution I settle on.

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

#17

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


#18

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.


#19

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]]);

#20

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]


#21

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!


#22

Hey countercoded,

Sure, I’ll give it a go.

I actually posted an overly-long post about array methods (including map) here:

In a nutshell, the map method returns a value for each element in an array. In the case of this challenge, you have an array of arrays (an array where each element in the array is itself an array). Those arrays contain, in turn, numbers.

The challenge is to return an array containing the largest number of each element in the main array.

The .map() method goes over each element in an array returns an array of results, so that’s useful.
The Math.max method accepts arguments which are numbers and returns the largest, so that’s useful as well.
The .apply() method is available on functions and will call the function allowing you to pass in an array which it will in turn use as arguments to the function being “applied”. That means you can pass in an array of numbers and Math.max() will return the largest.

Hopefully that makes sense.

Here are some references:

If you’re brand new to programming, this may be a bit much. But at the very least, it’s good to know tools like this exist so that you can explore.

Hope this helped!

Regards,
Micheal


#23

Here’s my solution, with a while loop:

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

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

#24

Hi all, just thought I’ll share my code here as well :slight_smile: Cheers!

function largestOfFour(arr) {
  
  // variable to contain new sorted array
  var biggestNumArr = [];
  
  // for loop to loop through each outer array element
  for (var i = 0; i < arr.length; i++) {
    
    // inner array elements sorted in reverse, and then
    // pushed into biggestNumArr
    biggestNumArr.push(arr[i].sort(function (a, b) {
      return b - a;
    })[0]);
  
  }
  
  return biggestNumArr;
}

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