Python program to remove rows with duplicate element in Matrix

Given Matrix, remove all rows which have duplicate elements in them.
Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]]
Output : [[4, 3, 2]]
Explanation : [4, 3, 2] is the only unique row.Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]]
Output : []
Explanation : No unique row.
Method 1 : Using list comprehension + set() + len()
In this, we extract only the rows which remain in the same length after converting it into a set.
Python3
| # initializing listtest_list =[[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]# printing original listprint("The original list is : "+str(test_list))# set() removing all elements# list comprehension used to filterres =[sub forsub intest_list iflen(set(sub)) ==len(sub)]# printing resultprint("Rows after removal : "+str(res)) | 
The original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]] Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(1) additional space is not needed.
Method #2 : Using filter() + lambda + set() + len()
In this, we perform the task of filtering using filter() + lambda function, and set and len() are used to check.
Python3
| # initializing listtest_list =[[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]# printing original listprint("The original list is : "+str(test_list))# set() removing all elements# filter() used to filterres =list(filter(lambdaele: len(set(ele)) ==len(ele), test_list))# printing resultprint("Rows after removal : "+str(res)) | 
The original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]] Rows after removal : [[4, 3, 2], [2, 4, 5]]
Method #3: Using Counter() function
Python3
| fromcollections importCounter# initializing listtest_list =[[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]# printing original listprint("The original list is : "+str(test_list))fori intest_list.copy():  freq =Counter(i)  if(len(freq)!=len(i)):    test_list.remove(i)    # printing resultprint("Rows after removal : "+str(test_list)) | 
The original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]] Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time Complexity:O(N*N)
Auxiliary Space :O(N)
Method #4: Using for loop and an if condition
Python3
| # initializing listtest_list =[[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]# printing original listprint("The original list is : "+str(test_list))res =[]forsub intest_list:    iflen(set(sub)) ==len(sub):        res.append(sub)# printing resultprint("Rows after removal : "+str(res))#This code is contributed by Vinay Pinjala. | 
The original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]] Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time Complexity:O(N*NlogN)
Auxiliary Space :O(N)
Method#5: Using Recursive method.
Algorithm:
- Check if the list is empty. If it is, return an empty list.
- Check if the first sub-list in the list has duplicate elements. If it doesn’t, include it in the result list and call the function recursively with the remaining sub-lists.
- If it does have duplicate elements, skip it and call the function recursively with the remaining sub-lists.
- Return the result list.
Python3
| defremove_duplicate_rows(test_list):    ifnottest_list:        return[]    # check if the first sub-list has duplicate elements    iflen(set(test_list[0])) ==len(test_list[0]):        # if not, include it in the result and call the function recursively with the remaining sub-lists        return[test_list[0]] +remove_duplicate_rows(test_list[1:])    else:        # if it has duplicate elements, skip it and call the function recursively with the remaining sub-lists        returnremove_duplicate_rows(test_list[1:])# initializing listtest_list =[[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]# printing original listprint("The original list is : "+str(test_list))res =remove_duplicate_rows(test_list)# printing resultprint("Rows after removal : "+str(res))#This code is contributed by tvsk. | 
The original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]] Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time complexity: O(n^2), where n is the total number of elements in the list. This is because we need to compare each sub-list with every other sub-list to determine if it has duplicate elements.
Auxiliary Space: O(n), where n is the total number of elements in the list. This is because we are creating a new list to store the non-duplicate sub-lists.
Method 6: Using the built-in all() function:
Step-by-step approach:
- Initialize an empty list to store the rows that don’t have duplicate elements.
- Use a list comprehension to iterate over each row of the input list.
- For each row, create a set and check if the length of the set is equal to the length of the row using the all() function.
- If all() elements in the row are unique, append the row to the list from step 1.
 
- Print the resulting list.
Below is the implementation of the above approach:
Python3
| # initializing listtest_list =[[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]# printing original listprint("The original list is : "+str(test_list))# using all() function to check if all elements are uniqueres =[sub forsub intest_list ifall(sub.count(elem) ==1forelem insub)]# printing resultprint("Rows after removal : "+str(res)) | 
The original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]] Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time complexity: O(n*m), where n is the number of rows and m is the maximum length of a row.
Auxiliary space: O(k), where k is the number of rows that don’t have duplicate elements.
 
				 
					


