# Find duplicate rows in a binary matrix in C++

C++Server Side ProgrammingProgramming

Suppose we a binary matrix. Here we will see how to find the duplicate rows in that matrix. Suppose the matrix is like −

 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

There are duplicate rows at position 3, 4, 5.

To solve this, we will use the Trie. The Trie is an efficient data structure used for strong and retrieval of data where character set is small. The search complexity is optimal as the key length. So at first we will insert the binary trie. If the newly added row is already present, then that is duplicate.

## Example

Live Demo

#include<iostream>
using namespace std;
const int MAX = 100;
class Trie {
public:
bool leaf_node;
Trie* children;
};
Trie* getNode() {
Trie* node = new Trie;
node->children = node->children = NULL;
node->leaf_node = false;
return node;
}
bool insert(Trie*& head, bool* arr, int N) {
for (int i = 0; i < N; i++) {
if (curr->children[arr[i]] == NULL)
curr->children[arr[i]] = getNode();
curr = curr->children[arr[i]];
}
if (curr->leaf_node)
return false;
return (curr->leaf_node = true);
}
void displayDuplicateRows(bool matrix[][MAX], int M, int N) {
for (int i = 0; i < M; i++)
cout << "There is a duplicate row at position: "<< i << endl;
}
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},
};
displayDuplicateRows(mat, 6, 6);
}



### Output

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