Javatpoint Logo
Javatpoint Logo

Print unique rows in a given Boolean matrix


Boolean matrices are mathematical structures that consist of only two values, typically 0 and 1. These matrices are widely used in various fields, including computer science, image processing, and pattern recognition. One common task when working with Boolean matrices is to identify and print unique rows, which can be essential for data analysis and manipulation.

Understanding Boolean Matrices:

A Boolean matrix is a two-dimensional array that contains only Boolean values, i.e., either 0 or 1. Each element of the matrix represents a binary decision or state. Boolean matrices are commonly employed to represent relationships, connectivity, or patterns in different applications. For example, a boolean matrix could represent the adjacency matrix of a graph, where each entry indicates whether there is an edge between two vertices.

Importance of Unique Rows:

In many applications, boolean matrices are used to store information about a set of objects or conditions. Identifying and printing unique rows in such matrices is essential for removing redundancy and obtaining a concise representation of the underlying data. This process is particularly valuable when dealing with large datasets, as it helps in simplifying and optimizing subsequent analyses.

Problem Statement:

Given a Boolean matrix, the objective is to identify and print only the unique rows present in the matrix. Two rows are considered identical if they have the same elements in the same order. The uniqueness of rows becomes crucial in scenarios where duplicate information needs to be eliminated for efficient analysis and representation.

The Challenge:

Consider a scenario where you are presented with a Boolean matrix, possibly representing a dataset or a logical configuration. The challenge is to identify and print only the unique rows in this matrix. A unique row is one that does not have an identical counterpart in the matrix. In simpler terms, we want to filter out rows that are duplicates and print only those that stand alone.

Naive Approach:

The simplest way to achieve this task is to iterate through each row and compare it with all other rows, checking for uniqueness. While this approach works, it is highly inefficient, especially for large matrices, as it has a time complexity of O(n^2).

Algorithm to Print Unique Rows:

Let's outline a simple algorithm to print unique rows in a boolean matrix using C++:

  • Initialize an unordered set to store unique rows.
  • Traverse each row of the matrix.
  • Convert the row into a string representation.
  • Check if the string representation is already present in the set.
    1. If present, continue to the next row.
    2. If not present, print the row and add the string representation to the set.

Repeat steps 2-4 for all rows in the matrix.



  • In the printUniqueRows function, a unordered_set named uniqueRows is created to store unique row identifiers.
  • The function takes a reference to a 2D vector (matrix) representing the boolean matrix as its parameter.
  • It iterates over each row in the matrix, converting it into a unique identifier (rowIdentifier).
  • This conversion is achieved by treating each element in the row as a binary digit, and bitwise shifting the existing identifier to the left by one position for each new element, effectively creating a unique binary representation for each row.
  • The program checks whether the computed rowIdentifier is already present in the uniqueRows If not, it prints the current row to the console and inserts the identifier into the set.
  • This process ensures that only unique rows are printed, and duplicates are ignored.
  • The main function demonstrates the program's functionality with an example boolean matrix named
  • This matrix contains a few rows with repeated patterns. The program prints the unique rows, eliminating any redundancy.

Program Output:

Print unique rows in a given Boolean matrix


In conclusion, the problem of printing unique rows in a given boolean matrix involves efficiently identifying and displaying the distinct rows present in the matrix. This task is crucial in various applications, such as data processing and pattern recognition, where the elimination of redundant information is essential for optimal performance.

Addressing this challenge often requires a systematic approach that leverages data structures or algorithms capable of efficiently identifying and storing unique rows. By carefully implementing such methods, we can significantly improve the efficiency of the solution, ensuring that the uniqueness of rows is determined in an optimal manner.

Furthermore, the significance of this problem extends beyond its immediate application in boolean matrices. The underlying principles and techniques used to solve this problem can be adapted and applied in a broader context, showcasing the versatility of algorithmic solutions in solving diverse computational challenges.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA