
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Matrix is Symmetric in JavaScript
To check if a matrix is symmetric, we simply check if the matrix and it's corresponding transpose matrix are equal. A symmetric matrix is a special case of a matrix where both the matrix and the transpose of the matrix are the same (A = A^T).
In this article we are given with an array and our task is to write a JavaScript program to check if a matrix is symmetric. Users must be familiar with 2D matrix, transpose of matrix, symmetric matrix, nested loop and if/else statement.

Example
Input: matrix [A]: [[1, 2, 3], [2, 3, 8], [3, 8, 9]] transpose([A]): [[1, 2, 3], [2, 3, 8], [3, 8, 9]] Output: true, symmetric Input: matrix [A] = [[1, 2, 3], [2, 3, 8], [3, 9, 0]] transpose([A]) = [[1, 2, 3], [2, 3, 9], [3, 8, 0]] Output: false, not symmetric
Approaches to Check if a Matrix is Symmetric
Here is a list of approaches to check if a matrix is symmetric in JavaScript which we will be discussing in this article with stepwise explanation and complete example codes.
By Comparing with Transpose Matrix
To check if a matrix is symmetric, we have created a transpose matrix of the given matrix and then compared each element of given matrix with the transpose matrix.
- We have declared a 2D array mat and two functions, getTranspose() and check(). Both the functions accept mat as argument.
- The getTranspose() function creates the transpose of the given matrix. First we check if the number of rows and columns are same to check if given matrix is square or not.
- Then we create a new array of m rows using Array() object and store it in transP.
- Then we have created a nested for loop where outer for loop creates row for the transpose matrix and inner loop creates the columns. We have created a new array cur, it stores the values of the transposed row corresponding to ith column in original matrix.
- Inside inner loop, for each j we have assigned mat[j][i] at row j and column i in the original matrix to cur[j] corresponding element in ith row of transposed matrix.
- After inner loop ends, we assign the newly created row cur to transP. Then, transpose matrix transP is returned.
- In the check(), first we get the transpose matrix by calling getTranspose() and then compare the matrix mat with transpose matrix transP using if/else statement and nested for loop.
- Then if/else is used to print the result according to check() function in web console using console.log().
Example
Here is a complete example code implementing above mentioned steps to check if a matrix is symmetric in JavaScript by comparing with transpose matrix.
var mat = [[1, 2, 3], [2, 3, 8], [3, 8, 9]] console.log("The given matrix is: ") console.log(mat); function getTranspose(mat) { var n = mat.length; var m = mat[0].length; if (n != m) { return false; } var transP = new Array(m) for (var i = 0; i < m; i++) { var cur = new Array(n); for (var j = 0; j < n; j++) { cur[j] = mat[j][i]; } transP[i] = cur; } return transP; } function check(mat) { var n = mat.length; var m = mat[0].length; if (n != m) { return false; } var transP = getTranspose(mat); for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { if (mat[i][j] != transP[i][j]) { return false; } } } return true; } if (check(mat)) { console.log("The given matrix is a symmetric matrix") } else { console.log("The given matrix is not a symmetric matrix") }
Using Nested Loop
In this approach to check if a matrix is symmetric, we have used nested loop to compare the given matrix without creating a transpose matrix.
- We have declared a 2D array mat and defined a function check() that accepts mat as argument.
- Inside the function check(), first we get the number of rows and columns using length property and then checked if the given matrix is square matrix or not.
- We have used nested for loop where outer loop iterates over rows and inner loop iterates over the column.
- Then we have used if/else statement to check mat[i][j] != mat[j][i]. Here we are comparing the elements of rows with columns and the element of columns with rows. If elements of rows or columns is not correspondigly equal to columns or rows of matrix that means given matrix is not symmetric matrix and returns false and returns true after completion of loop meaning matrix is symmetric.
- The if/else is used to print the result according to check() function in web console using console.log().
Example
Here is a complete example code implementing above mentioned steps to check if a matrix is symmetric in JavaScript using nested loop.
var mat = [[1, 2, 3], [2, 3, 8], [3, 9, 0]] console.log("The given matrix is: ") console.log(mat); function check(mat) { var n = mat.length; var m = mat[0].length; if (n != m) { return false; } for (var i = 0; i < n; i++) { for (var j = 0; j < i; j++) { if (mat[i][j] != mat[j][i]) { return false; } } } return true; } if (check(mat)) { console.log("The given matrix is a symmetric matrix") } else { console.log("The given matrix is not a symmetric matrix") }
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Transpose Matrix Comparison | O(n^2) | O(n^2) |
Nested Loop | O(n^2) | O(1) |
Conclusion
In this article to check if a matrix is symmetric in JavaScript we have discussed two different approaches. These approaches are: by comparing with transpose matrix and using nested loop.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.