Checking for particular types of matrix in JavaScript

We need to write a JavaScript function that checks if every diagonal from top-left to bottom-right in a 2D matrix has identical elements. This is useful for pattern recognition and matrix analysis tasks.

Problem Statement

Given a 2D array, our function should verify that all elements in each diagonal (running from top-left to bottom-right) are the same. If this condition is met for all diagonals, return true, otherwise false.

For example, consider this matrix:

const arr = [
   [6, 7, 8, 9],
   [2, 6, 7, 8],
   [1, 2, 6, 7],
];

The diagonals are: [1], [2,2], [6,6,6], [7,7,7], [8,8], [9]. Since each diagonal contains identical elements, the result is true.

Understanding the Diagonals

6 7 8 9 2 6 7 8 1 2 6 7 Matrix Diagonals Red: [1] Blue: [2,2] Green: [6,6,6] Purple: [7,7,7] Orange: [8,8] Brown: [9]

Solution Implementation

The algorithm works by checking each diagonal starting from the top row and left column. For each starting position, we validate that all elements along that diagonal are identical.

const arr = [
   [6, 7, 8, 9],
   [2, 6, 7, 8],
   [1, 2, 6, 7],
];

const checkMatrix = (arr = []) => {
   const validate = (row, col) => {
      while (
         row < arr.length
         && col < arr[0].length
         && arr[row + 1]
         && arr[row + 1][col + 1] !== undefined
      ) {
         if (arr[row + 1][col + 1] !== arr[row][col]) {
            return false
         }
         row += 1
         col += 1
      }
      return true
   }
   
   // Check diagonals starting from top row
   for (let i = 0; i < arr[0].length; i++) {
      if (!validate(0, i)) {
         return false
      }
   }
   
   // Check diagonals starting from left column
   for (let i = 0; i < arr.length; i++) {
      if (!validate(i, 0)) {
         return false
      }
   }
   
   return true
}

console.log(checkMatrix(arr));
true

How It Works

The solution uses two key components:

  • validate function: Checks if all elements in a diagonal starting from position (row, col) are identical
  • Main loops: Iterate through all possible diagonal starting positions - first row and first column

The function traverses each diagonal by incrementing both row and column indices simultaneously, comparing adjacent elements until reaching the matrix boundary.

Testing with Different Cases

// Test case 1: Valid matrix
const validMatrix = [
   [1, 2, 3],
   [4, 1, 2],
   [5, 4, 1]
];

// Test case 2: Invalid matrix
const invalidMatrix = [
   [1, 2, 3],
   [4, 5, 2],
   [7, 4, 1]
];

console.log("Valid matrix:", checkMatrix(validMatrix));
console.log("Invalid matrix:", checkMatrix(invalidMatrix));
Valid matrix: true
Invalid matrix: false

Conclusion

This algorithm efficiently checks diagonal consistency in a 2D matrix by validating each diagonal from top-left to bottom-right. It's useful for pattern recognition and mathematical matrix operations where diagonal uniformity is required.

Updated on: 2026-03-15T23:19:00+05:30

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements