VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-unique-rows/

⇱ Print unique rows in a given Binary matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print unique rows in a given Binary matrix

Last Updated : 23 Jul, 2025

Given a binary matrix, print all unique rows of the given matrix. 

Example: 

Input:
 {0, 1, 0, 0, 1}
 {1, 0, 1, 1, 0}
 {0, 1, 0, 0, 1}
 {1, 1, 1, 0, 0}
Output:
 0 1 0 0 1 
 1 0 1 1 0 
 1 1 1 0 0 
Explanation: 
The rows are r1={0, 1, 0, 0, 1}, 
r2={1, 0, 1, 1, 0}, r3={0, 1, 0, 0, 1}, 
r4={1, 1, 1, 0, 0}, As r1 = r3, remove r3
and print the other rows.

Input:
 {0, 1, 0}
 {1, 0, 1}
 {0, 1, 0}
Output:
 0 1 0
 1 0 1
Explanation: 
The rows are r1={0, 1, 0}, 
r2={1, 0, 1}, r3={0, 1, 0} As r1 = r3,
remove r3 and print the other rows.

: This method explains the simple approach towards solving the above problem. 

Approach: A simple approach would be to check each row with all processed rows. Print the first row. Now, starting from the second row, for each row, compare the row with already processed rows. If the row matches with any of the processed rows, skip it else print it.

Algorithm: 

  1. Traverse the matrix row-wise
  2. For each row check if there is any similar row less than the current index.
  3. If any two rows are similar then do not print the row.
  4. Else print the row.

Implementation: 


Output
0 1 0 0 1 
1 0 1 1 0 
1 0 1 0 0 

Complexity Analysis: 

  • Time complexity: O( ROW^2 x COL ). 
    So for every row check if there is any other similar row. So the time complexity is O( ROW^2 x COL ).
  • Auxiliary Space: O(1). 
    As no extra space is required.


: This method uses Binary Search Tree to solve the above operation. The Binary Search Tree is a node-based binary tree data structure which has the following properties: 

  • The left subtree of a node contains only nodes with keys lesser than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • The left and right subtree each must also be a binary search tree.
  • There must be no duplicate nodes.

The above properties of Binary Search Tree provide ordering among keys so that the operations like search, minimum and maximum can be done fast. If there is no order, then we may have to compare every key to search a given key.

Approach: The process must begin from finding the decimal equivalent of each row and inserting them into a BST. As we know, each node of the BST will contain two fields, one field for the decimal value, other for row number. One must not insert a node if it is duplicated. Finally, traverse the BST and print the corresponding rows.

Algorithm: 

  1. Create a BST in which no duplicate elements can be stored. Create a function to convert a row into decimal and to convert the decimal value into binary array.
  2. Traverse through the matrix and insert the row into the BST.
  3. Traverse the BST (inorder traversal) and convert the decimal into binary array and print it.

Implementation: 


Output
1 0 1 0 0 
1 0 1 1 0 
0 1 0 0 1 


Complexity Analysis: 

  • Time complexity: O( ROW x COL + ROW x log( ROW ) ). 
    To traverse the matrix time complexity is O( ROW x COL) and to insert them into BST time complexity is O(log ROW) for each row. So overall time complexity is O( ROW x COL + ROW x log( ROW ) )
  • Auxiliary Space: O( ROW ). 
    To store the BST O(ROW) space is needed.


: This method uses Trie data structure to solve the above problem. Trie is an efficient information retrieval data structure. Using Trie, search complexities can be brought to an optimal limit (key length). If we store keys in the binary search tree, a well-balanced BST will need time proportional to M * log N, where M is maximum string length and N is the number of keys in the tree. Using Trie, we can search the key in O(M) time. However, the penalty is on Trie storage requirements.

Note: This method will lead to Integer Overflow if the number of columns is large. 

Approach: 
Since the matrix is boolean, a variant of Trie data structure can be used where each node will be having two children one for 0 and other for 1. Insert each row in the Trie. If the row is already there, don't print the row. If the row is not there in Trie, insert it in Trie and print it.

Algorithm: 

  1. Create a Trie where rows can be stored.
  2. Traverse through the matrix and insert the row into the Trie.
  3. Trie cannot store duplicate entries so the duplicates will be removed
  4. Traverse the Trie and print the rows.

Implementation: 


Output
0 1 0 0 1 
1 0 1 1 0 
1 0 1 0 0 

Complexity Analysis: 

  • Time complexity: O( ROW x COL ). 
    To traverse the matrix and insert in the trie the time complexity is O( ROW x COL). This method has better time complexity. Also, the relative order of rows is maintained while printing but it takes a toll on space.
  • Auxiliary Space: O( ROW x COL ). 
    To store the Trie O(ROW x COL) space complexity is needed.


: This method uses HashSet data structure to solve the above problem. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The class offers constant time performance for the basic operations like add, remove, contains and size assuming the hash function disperses the elements properly among the buckets. 

Approach: In this method convert the whole row into a single String and then if check it is already present in the HashSet or not. If the row is present then we will leave it otherwise we will print unique row and add it to HashSet.

Algorithm: 

  1. Create a HashSet where rows can be stored as a String.
  2. Traverse through the matrix and insert the row as String into the HashSet.
  3. HashSet cannot store duplicate entries so the duplicates will be removed
  4. Traverse the HashSet and print the rows.

Implementation: 


Output
01001
10110
11100

Complexity Analysis: 

  • Time complexity: O( ROW x COL ). 
    To traverse the matrix and insert in the HashSet the time complexity is O( ROW x COL)
  • Auxiliary Space: O( ROW ). 
    To store the HashSet O(ROW x COL) space complexity is needed.
Comment