
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find duplicate rows in a binary matrix in C++
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
#include<iostream> using namespace std; const int MAX = 100; class Trie { public: bool leaf_node; Trie* children[2]; }; Trie* getNode() { Trie* node = new Trie; node->children[0] = node->children[1] = NULL; node->leaf_node = 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]] = 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) { Trie* head = getNode(); for (int i = 0; i < M; i++) if (!insert(head, matrix[i], N)) 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
- Related Articles
- Write Python program to find duplicate rows in a binary matrix
- Find pair of rows in a binary matrix that has maximum bit difference in C++
- Python program to remove rows with duplicate element in Matrix
- Nearest 1 in a binary matrix in C++
- Find distinct elements common to all rows of a Matrix in C++
- Count all sorted rows in a matrix in C++
- Find Number of Sorted Rows in a Matrix in Java?
- How to find and filter Duplicate rows in Pandas ?
- Reconstruct a 2-Row Binary Matrix in C++
- Find the Kth Smallest Sum of a Matrix With Sorted Rows in C++
- Shortest Path in Binary Matrix in C++
- Find maximum path length in a binary matrix in Python
- Find perimeter of shapes formed with 1s in binary matrix in C++
- Find a common element in all rows of a given row-wise sorted matrix in C++
- Maximum decimal value path in a binary matrix in C++

Advertisements