VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-duplicate-rows-binary-matrix/

⇱ Duplicate Rows in a Binary Matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Duplicate Rows in a Binary Matrix

Last Updated : 11 Apr, 2026

Given a binary matrix mat[][], print the rows which are duplicates of rows that are already present in the matrix.

Examples: 

Input: mat[][] = [[1, 1, 0, 1, 0, 1],
[0, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 0, 0],
[1, 1, 0, 1, 0, 1],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 0, 1]]
Output: [4, 5, 6]
Explanation: Row at index 4 is same as 0. Rows at indexes 5 and 6 are same as index 1

[Naive Approach] Traverse All Rows - O((m^2)*n) Time and O(1) Space

For each row, check if it already exists by scanning all previous rows.


Output
4 5 6 

[Expected Approach-1] Using Trie - O(m*n) Time and O(m*n) Space

A Trie is used to efficiently store rows of the binary matrix since each row contains only 0 and 1. Each row is inserted as a path in the Trie. If the path already exists and the final node is marked as a leaf, the row is identified as a duplicate, otherwise the row is inserted and the end node is marked.


Output
4 5 6 

[Expected Approach-2] Using a hash set of rows as strings: O(m*n) Time and O(m*n) Space

Use a hash set to store string representations of each row, allowing for quick lookup and comparison. By iterating through the rows and concatenating their elements into strings, then checks for duplicate strings in the hash set. If a duplicate is found, the index of the corresponding row is added to a result array.


Output
4 5 6 
Comment
Article Tags: