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

2
  • possible duplicate of stackoverflow.com/questions/1669190/… Commented Sep 16, 2015 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. Commented Mar 4, 2019 at 9:53

4 Answers 4

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"
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Additionally, as per stackoverflow.com/a/54980012/7438857, using a loop is actually more performant.
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

2 Comments

add more description
Easy way to find largest numbers in 2D array.
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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.