Javatpoint Logo
Javatpoint Logo

Print unique rows in a given Binary matrix in C++

The problem of printing unique rows in a given binary matrix in C++ can be understood and solved using several computer science concepts and theories. Here are some key theories and concepts that are relevant to solving this problem:

  1. Binary Matrix Representation
    • In a binary matrix, each element is either 0 or 1, and the matrix is shown as a two-dimensional array.
    • Rows in the binary matrix indicate binary sequences, in which 0 frequently equates to "false" and 1 typically equates to "true".
  2. Objective
    • Identifying and displaying just the rows that are unique or distinct inside the matrix is the goal of printing unique rows in a given binary matrix in C++.
  3. Structures for Data
    • A binary matrix is often represented by a C++ std::vector of std::vectorint>.
    • A std::unordered_set is commonly used to maintain track of distinct rows. The set maintains string representations of the rows to eliminate duplicates.
  4. Algorithm
    • Initialize a blank set first to store distinct row representations.
    • The binary matrix should be iterated over each row.
    • Make an individual depiction of each row's elements. To accomplish this, combine the binary values (0s and 1s) in the row to create a string.
    • To see if this representation is already present in the collection of distinctive row representations, perform the following check:
    • If it's not in the set, the row is distinctive. The representation is added to the set, and then the row is printed.
    • If it is already in the set previously, it indicates that the row is a duplicate, and you may exclude it.
    • For each row in the matrix, repeat this step.
  5. Output
    • The binary matrix's distinct rows are what the program returns as its output. These rows in the matrix are the only ones that do not replicate any other rows.
  6. Applications
    • In data processing, database management, and pattern recognition, locating unique rows in a binary matrix is a frequent problem. It can be used to remove duplicate records from a dataset or identify distinctive patterns therein.


Let's take an example to print unique rows in a given binary matrix in C++:


Unique rows in the binary matrix:
1 0 1 0 
1 1 0 0 
0 1 0 1


  1. A binary matrix is required as input for the program's defined function printUniqueRows.
  2. Unique row representations are kept in the function's internal std::unordered_set, which is called uniqueRows.
  3. Each row of the binary matrix is traversed by the program repeatedly.
  4. It changes the binary values for each row into a representation in a string (rowStr). This string symbolizes the binary row, which consists of a concatenation of the digits 1 and 0.
  5. The uniqueRows set is checked to see if the rowStr is already there. If not, it adds the rowStr to the set and outputs the singular binary row (i.e., if the row is not unique).
  6. The definition of a sample binary matrix (matrix) can be found in the main function.
  7. The program using the printUniqueRows function to prints the binary matrix's unique rows.
  8. The unique rows from the binary matrix will be shown in the output.

Using data structures to maintain track of unique row representations and iterating through the matrix to find and print the unique rows while avoiding duplicates are the final steps in printing unique rows in a binary matrix. This method is useful for many different data processing and analysis activities.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA