We will share a C++ program to find duplicate rows in a binary matrix. If you are a beginner and want to start learning the C++ programming, then keep your close attention in this tutorial as I am going to share a program for C++ program to find duplicate rows in a binary matrix with the output.
Copy the below C++ program and execute it with the help of GCC compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include<bits/stdc++.h> const int MAX = 100; struct Trie { bool leaf; Trie* children[2]; }; Trie* getNewTrieNode() { Trie* node = new Trie; node->children[0] = node->children[1] = NULL; node->leaf = false; return node; } bool insert(Trie*& head, bool* arr, int N) { Trie* curr = head; for (int i = 0; i < N; i++) { if (curr->children[arr[i]] == NULL) curr->children[arr[i]] = getNewTrieNode(); curr = curr->children[arr[i]]; } if (curr->leaf) return false; return (curr->leaf = true); } void printDuplicateRows(bool mat[][MAX], int M, int N) { Trie* head = getNewTrieNode(); for (int i = 0; i < M; i++) if (!insert(head, mat[i], N)) printf("There is a duplicate row" " at position: %d \n", i+1); } 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; } |
There is a duplicate row at position: 4
There is a duplicate row at position: 5
There is a duplicate row at position: 6
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.