Home > Mobile >  How to find the highest number in a 2-dimensional array?
How to find the highest number in a 2-dimensional array?

Time:09-16

Hi all and thanks in advance for your help.

So I would like to find the highest number in a 2-dimensional array. Below is the code:

const matrix = [
  [2, 56, 10],
  [20, 34, 10],
  [23, 144, 26]
];
let maximum = matrix[0][0];
for (var row = 0; row < matrix.length; row  ) {
  for (var col = 0; col < matrix.length; col  ) {
    if (matrix[row][col] > maximum) {
      maximum = matrix[row][col];
    };
  };
};
document.write(' -- ', maximum);

Here is my problem - Could you please help me to understand why when I have more numbers in the array I cannot see the highest number - Find below an example ):

const matrix = [
  [2, 56, 10, 14, 422, 3242],
  [20, 34, 55, 100, 33, 422],
  [23, 12, 14, 26, 203, 233]
];
let maximum = matrix[0][0];
for (var row = 0; row < matrix.length; row  ) {
  for (var col = 0; col < matrix.length; col  ) {
    if (matrix[row][col] > maximum) {
      maximum = matrix[row][col];
    };
  };
};
document.write(' -- ', maximum);

CodePudding user response:

row < matrix.length tests the correct thing. col < matrix.length does not: you should replace it with col < matrix[row].length.

However, there is an easier way, using some of the newer JavaScript features:

const matrix = [[2,56,10,14,422,3242],[20,34,55,100,33,422],[23,12,14,26,203,233]];
const maximum = Math.max(...matrix.flat())
console.log(maximum);

matrix.flat() will flatten the two-dimensional array into one dimension, ... syntax will put each value from the one-dimensional array as its own argument to Math.max, which then finds the biggest one.

CodePudding user response:

There is one small mistake. When you iterate the column make sure you iterate the number of columns.

matrix.length gives you the number of rows and matrix[i].length gives you the number of columns.

const matrix = [[2,56,10,14,422,3242],[20,34,55,100,33,422],[23,12,14,26,203,233]];
let maximum = matrix[0][0];
for(var row = 0; row < matrix.length; row  ){
  for(var col = 0; col < matrix[row].length; col  ){
    if(matrix[row][col] > maximum){
      maximum = matrix[row][col];
    };
  };  
};
document.write(' -- ', maximum);

CodePudding user response:

You are taking matrix.length for no. of column as well, but it gives you no. of rows i.e. 3 but in your case but no. of column is 6 . that's why it only check for 3 numbers


const matrix = [[2,56,10,14,422,3242],[20,34,55,100,33,422],[23,12,14,26,203,233]];
let maximum = matrix[0][0];
for(var row = 0; row < matrix.length; row  ){
  for(var col = 0; col < matrix[0].length; col  ){          <--- Correction
    if(matrix[row][col] > maximum){
      maximum = matrix[row][col];
    };
  };  
};
document.write(' -- ', maximum);

CodePudding user response:

Also can use Array.prototype.reduce() combined with Math.max():

const matrix = [[2,56,10,14,422,3242],[20,34,55,100,33,422],[23,12,14,26,203,233]]

const maximum = matrix.reduce((a, c) => Math.max(a, ...c), 0)
console.log(maximum)

  • Related