0

This is my attempt and it works fine, but I need to return the array containing the largest number, not the largest number itself.

Also, I want to know if there is a way to use deep-equal to compare inner arrays in 2D arrays.

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

 return largest;

}

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

console.log(largest);
squaleLis
  • 6,116
  • 2
  • 22
  • 30
dev_sahar
  • 13
  • 1
  • 2
  • possible duplicate of http://stackoverflow.com/questions/1669190/javascript-min-max-array-values – durbnpoisn Sep 16 '15 at 19:19
  • @durbnpoisn this is asking how to find the array with the largest number in a 2D array with Javascript, not the largest number of a (1D) array. – James Ray Mar 04 '19 at 09:53

4 Answers4

1

just for fun using new array methods:

  function largestOfFour(r){
    var biggest=Math.max.apply(null, r.join(",").split(",")); //merge arrays then find largest number in flat
    return  r.find( /./.test, RegExp("\\b"+biggest+"\\b") ); // find by matching element of sub-array to biggest flat array value
  }


  var largest=largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
  alert(largest); // shows "1000, 1001, 857, 1"
dandavis
  • 16,370
  • 5
  • 40
  • 36
  • thanks for your answer, but I guess that is a little complex for me. I am still learning JavaScript so loops are the easiest way for me at least for now. – dev_sahar Sep 16 '15 at 19:47
  • Additionally, as per https://stackoverflow.com/a/54980012/7438857, using a loop is actually more performant. – James Ray Mar 04 '19 at 10:49
1

Easy way to find largest numbers in 2D array

function largestOfFour(arr) {
  return arr.map(n => Math.max(...n));
}

Function explanation:

  1. Array's map method will iterate and execute the callback on each item and populate the array with the computed value
  2. Math's max method will get the biggest value in the given set of values
  3. Once the max value computed it will be pushed to the array arr
gvgvgvijayan
  • 1,851
  • 18
  • 34
Zhivko Zhelev
  • 321
  • 4
  • 10
0

Create another variable and store the arr[i] array at the same time that you note the biggest number. You could remove largest variable if you only need the largest array (or replace largestOrigin by it).

JavaScript

function largestOfFour(arr) {
    var largest = 0,
        largestOrigin = [];

    for(var i = 0; i < arr.length; i++){ 
        for (var j = 0; j < arr[i].length; j++) {
            if(arr[i][j] > largest){
                largest = arr[i][j];
                largestOrigin = arr[i];
            }
        }
    }
    return largestOrigin;
}

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

JSFiddle (live demo)

Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
0

I'm also working on those freecodecamp bonfires :). Here was my solution:

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

  return max;
}
TylerMayfield
  • 83
  • 1
  • 1
  • 9