
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write Python program to find duplicate rows in a binary matrix
Given a binary matrix contains 0 and 1, our task is to find duplicate rows and print it.
Python provides Counter() method which is used here.
Example
Input: 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 Output: (1, 1, 1, 1) (0, 0, 0, 0)
Algorithm
Step 1: Create a binary matrix, only 0 and 1 elements are present. Step 2: Which will have rows as key and it’s frequency as value. Lists are mutable so first, we will cast each row (list) into a tuple. Step 3: Create a dictionary using the counter method. Step 4: Now traverse the dictionary completely. Step 5: Print all rows which have frequency greater than 1.
Example Code
# Function to find duplicate rows in a binary matrix from collections import Counter def binarymatrix(A): A = map(tuple,A) dic = Counter(A) print("Duplicate rows of Binary Matrix ::>") for (i,j) in dic.items(): if j>1: print (i) # Driver program if __name__ == "__main__": A=[] n=int(input("Enter n for n x n matrix : ")) #3 here #use list for storing 2D array #get the user input and store it in list (here IN : 1 to 9) print("Enter the element ::>") for i in range(n): row=[] #temporary list to store the row for j in range(n): row.append(int(input())) #add the input to row list A.append(row) #add the row to the list print(A) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] #Display the 2D array print("Display Array In Matrix Form") for i in range(n): for j in range(n): print(A[i][j], end=" ") print() binarymatrix(A)
Output
Enter n for n x n matrix : 4 Enter the element ::> 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 [[1, 1, 1, 1], [0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0]] Display Array In Matrix Form 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 Duplicate rows of Binary Matrix ::> (1, 1, 1, 1) (0, 0, 0, 0)
- Related Articles
- Find duplicate rows in a binary matrix in C++
- Python program to remove rows with duplicate element in Matrix
- Write a program in Python to remove first duplicate rows in a given dataframe
- Program to find number of special positions in a binary matrix using Python
- Write a Golang program to find duplicate elements in a given array
- Write a Golang program to find duplicate elements in a given range
- Program to find matrix for which rows and columns holding sum of behind rows and columns in Python
- Find distinct elements common to all rows of a matrix in Python
- Find maximum path length in a binary matrix in Python
- Python program to find all duplicate characters in a string
- Program to find column index where left most 1 is present in a binary matrix in Python?
- Find pair of rows in a binary matrix that has maximum bit difference in C++
- Python Program to sort rows of a matrix by custom element count
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Write a Python program to find the maximum value from first four rows in a given series

Advertisements