Given a binary matrix whose elements are only 0 and 1, we need to print the rows which are duplicate of rows which are already present in the matrix.

Examples:

Input : {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 : There is a duplicate row at position: 4 There is a duplicate row at position: 5 There is a duplicate row at position: 6

This problem is mainly an extension of find unique rows in a binary matrix.

A **Simple Solution** is to traverse all rows one by one. For every row, check if it is present anywhere else. If yes print the row.

Time complexity : O(ROW^2 x COL)

Auxiliary Space : O(1)

**Optimal solution using Trie** Trie is an efficient data structure used for strong and retrieval of data where character set is small. The searching complexity is optimal as key length.

The solution approach towards the question is to first insert the matrix in the binary trie and then if the new added row is already present in the trie then we will now that it is a duplicate row

`// C++ program to find duplicate rows ` `// in a binary matrix. ` `#include<bits/stdc++.h> ` ` ` `const` `int` `MAX = 100; ` ` ` `/*struct the Trie*/` `struct` `Trie ` `{ ` ` ` `bool` `leaf; ` ` ` `Trie* children[2]; ` `}; ` ` ` `/*function to get Trienode*/` `Trie* getNewTrieNode() ` `{ ` ` ` `Trie* node = ` `new` `Trie; ` ` ` `node->children[0] = node->children[1] = NULL; ` ` ` `node->leaf = ` `false` `; ` ` ` `return` `node; ` `} ` ` ` `/* function to insert a row in Trie*/` `bool` `insert(Trie*& head, ` `bool` `* arr, ` `int` `N) ` `{ ` ` ` `Trie* curr = head; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `/*creating a new path if it don not exist*/` ` ` `if` `(curr->children[arr[i]] == NULL) ` ` ` `curr->children[arr[i]] = getNewTrieNode(); ` ` ` ` ` `curr = curr->children[arr[i]]; ` ` ` `} ` ` ` ` ` `/*if the row already exist return false*/` ` ` `if` `(curr->leaf) ` ` ` `return` `false` `; ` ` ` ` ` `/* making leaf node tree and return true*/` ` ` `return` `(curr->leaf = ` `true` `); ` `} ` ` ` `void` `printDuplicateRows(` `bool` `mat[][MAX], ` `int` `M, ` `int` `N) ` `{ ` ` ` `Trie* head = getNewTrieNode(); ` ` ` ` ` `/*inserting into Trie and checking for dulpicates*/` ` ` `for` `(` `int` `i = 0; i < M; i++) ` ` ` ` ` `// If already exists ` ` ` `if` `(!insert(head, mat[i], N)) ` ` ` `printf` `(` `"There is a duplicate row"` ` ` `" at position: %d \n"` `, i+1); ` ` ` `} ` ` ` `/*driver function to check*/` `int` `main() ` `{ ` ` ` `bool` `mat[][MAX] = ` ` ` `{ ` ` ` `{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}, ` ` ` `}; ` ` ` ` ` `printDuplicateRows(mat, 6, 6); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

There is a duplicate row at position: 4 There is a duplicate row at position: 5 There is a duplicate row at position: 6

**Another approach without using Trie but does not work for large number of columns**

Another approach is be to convert the decimal equivalent of row and check if a new row has the same decimal equivalent then it is a duplicate row. It will not work if the number of columns is large .

This article is contributed by **Pranav**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Python Counter| Find duplicate rows in a binary matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Check if the rows of a binary matrix can be made unique by removing a single column
- Find a common element in all rows of a given row-wise sorted matrix
- Find all permuted rows of a given row in a matrix
- Find distinct elements common to all rows of a matrix
- Common elements in all rows of a given matrix
- Count all sorted rows in a matrix
- Maximum difference of sum of elements in two rows in a matrix
- Check if all rows of a matrix are circular rotations of each other
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Interchange elements of first and last rows in matrix
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Count rows in a matrix that consist of same element
- Remove first X rows and columns from a matrix
- Remove any corner X rows and columns from a matrix
- Number of rows and columns in a Matrix that contain repeated values
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged