Check Identical Matrices

Last Updated : 2 Jul, 2026

Given two square matricesĀ mat1[][]Ā andĀ mat2[][]Ā of sizeĀ n Ɨ n, determine whether the matrices are identical or not.

Examples:

Input: n = 2,Ā 
mat1[][] = [[1, 2], [3, 4]]
mat2[][] = [[1, 2], [3, 4]]
Output: true
Explanation: Both the matrices are identical, so the answer is 1.

Input: n = 2,
mat1[][] = [[1, 2], [3, 4]]
mat2[][] = [[1, 2], [3, 2]]
Output: false
Explanation: Both the matrices are not identical, So, answer is 0.

Try It Yourself
redirect icon

Traverse Both Matrices Simultaneously - O(n ^ 2) Time and O(1) Space

The idea is to traverse both matrices simultaneously and compare the corresponding elements. As soon as a mismatch is found, return false immediately without checking the remaining elements. If the traversal completes without finding any mismatch, return true.

Let us understand with an example:
Input: n = 2,Ā  mat1[][] = [[1, 2], [3, 4]], mat2[][] = [[1, 2], [3, 4]]

  • Start traversing both matrices simultaneously from the first element (0, 0).
  • Compare corresponding elements: 1 with 1, 2 with 2, 3 with 3, and 4 with 4. All pairs are equal, so continue the traversal.
  • No mismatch is encountered during the entire traversal.
  • After checking all n Ɨ n elements, the loops complete successfully.
  • Therefore, the function returns true, indicating that the two matrices are identical.
C++
#include <bits/stdc++.h>
using namespace std;

bool identicalMat(vector<vector<int>> mat1, vector<vector<int>> mat2)
{

    // iterating over each row and column of the matrices.
    int n = mat1.size();
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)

            // checking whether correspoding elements are same or not
            if (mat1[i][j] != mat2[i][j])
                return false;
    return true;
}

int main()
{
    int n = 2;
    vector<vector<int>> mat1 = {{1, 2}, {3, 4}};
    vector<vector<int>> mat2 = {{1, 2}, {3, 4}};

    cout << (identicalMat(mat1, mat2) ? "true" : "false");

    return 0;
}
Java
import java.util.Arrays;

public class GFG {
    public static boolean identicalMat(int[][] mat1,
                                       int[][] mat2)
    {

        // iterating over each row and column of the
        // matrices.
        int n = mat1.length;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {

                // checking whether corresponding elements
                // are same or not
                if (mat1[i][j] != mat2[i][j])
                    return false;
            }
        return true;
    }

    public static void main(String[] args)
    {
        int[][] mat1 = { { 1, 2 }, { 3, 4 } };
        int[][] mat2 = { { 1, 2 }, { 3, 4 } };

        System.out.println(
            identicalMat(mat1, mat2) ? "true" : "false");
    }
}
Python
def identicalMat(mat1, mat2):

    # iterating over each row and column of the matrices.
    n = len(mat1)
    for i in range(n):
        for j in range(n):

            # checking whether corresponding elements are same or not
            if mat1[i][j] != mat2[i][j]:
                return False
    return True


if __name__ == "__main__":
    mat1 = [[1, 2], [3, 4]]
    mat2 = [[1, 2], [3, 4]]

    print("true" if identicalMat(mat1, mat2) else "false")
C#
using System;

public class GFG {
    public static bool identicalMat(int[][] mat1,
                                    int[][] mat2)
    {
        int n = mat1.Length;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < mat1[i].Length; j++) {
                if (mat1[i][j] != mat2[i][j])
                    return false;
            }
        }

        return true;
    }

    public static void Main()
    {
        int[][] mat1
            = { new int[] { 1, 2 }, new int[] { 3, 4 } };

        int[][] mat2
            = { new int[] { 1, 2 }, new int[] { 3, 4 } };

        Console.WriteLine(
            identicalMat(mat1, mat2) ? "true" : "false");
    }
}
JavaScript
function identicalMat(mat1, mat2)
{

    // iterating over each row and column of the matrices.
    let n = mat1.length;
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {

            // checking whether corresponding elements are
            // same or not
            if (mat1[i][j] !== mat2[i][j]) {
                return false;
            }
        }
    }
    return true;
}

// Driver Code
const mat1 = [ [ 1, 2 ], [ 3, 4 ] ];
const mat2 = [ [ 1, 2 ], [ 3, 4 ] ];

console.log(identicalMat(mat1, mat2) ? "true" : "false");

Output
true
Comment