Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
How to find the highest number in a 2-dimensional array?

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!