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
test_list = [[ 4 , 3 , 2 ], [ 7 , 6 , 7 ], [ 2 , 4 , 5 ], [ 8 , 9 , 9 ]]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if len ( set (sub)) = = len (sub)]
print ( "Rows after removal : " + str (res))
|
OutputThe 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
test_list = [[ 4 , 3 , 2 ], [ 7 , 6 , 7 ], [ 2 , 4 , 5 ], [ 8 , 9 , 9 ]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda ele: len ( set (ele)) = = len (ele), test_list))
print ( "Rows after removal : " + str (res))
|
OutputThe 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
from collections import Counter
test_list = [[ 4 , 3 , 2 ], [ 7 , 6 , 7 ], [ 2 , 4 , 5 ], [ 8 , 9 , 9 ]]
print ( "The original list is : " + str (test_list))
for i in test_list.copy():
freq = Counter(i)
if ( len (freq)! = len (i)):
test_list.remove(i)
print ( "Rows after removal : " + str (test_list))
|
OutputThe 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
test_list = [[ 4 , 3 , 2 ], [ 7 , 6 , 7 ], [ 2 , 4 , 5 ], [ 8 , 9 , 9 ]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
if len ( set (sub)) = = len (sub):
res.append(sub)
print ( "Rows after removal : " + str (res))
|
OutputThe 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
def remove_duplicate_rows(test_list):
if not test_list:
return []
if len ( set (test_list[ 0 ])) = = len (test_list[ 0 ]):
return [test_list[ 0 ]] + remove_duplicate_rows(test_list[ 1 :])
else :
return remove_duplicate_rows(test_list[ 1 :])
test_list = [[ 4 , 3 , 2 ], [ 7 , 6 , 7 ], [ 2 , 4 , 5 ], [ 8 , 9 , 9 ]]
print ( "The original list is : " + str (test_list))
res = remove_duplicate_rows(test_list)
print ( "Rows after removal : " + str (res))
|
OutputThe 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
test_list = [[ 4 , 3 , 2 ], [ 7 , 6 , 7 ], [ 2 , 4 , 5 ], [ 8 , 9 , 9 ]]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all (sub.count(elem) = = 1 for elem in sub)]
print ( "Rows after removal : " + str (res))
|
OutputThe 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.