Thursday, July 4, 2024
HomeLanguagesPythonPython program to remove rows with duplicate element in Matrix

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 list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# set() removing all elements
# list comprehension used to filter
res = [sub for sub in test_list if len(set(sub)) == len(sub)]
 
# printing result
print("Rows after removal : " + str(res))


Output

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 list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# set() removing all elements
# filter() used to filter
res = list(filter(lambda ele: len(set(ele)) == len(ele), test_list))
 
# printing result
print("Rows after removal : " + str(res))


Output

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




from collections import Counter
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
 
# printing original list
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)
     
 
# printing result
print("Rows after removal : " + str(test_list))


Output

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 list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
for sub in test_list:
    if len(set(sub)) == len(sub):
        res.append(sub)
 
# printing result
print("Rows after removal : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

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:

  1. Check if the list is empty. If it is, return an empty list.
  2. 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.
  3. If it does have duplicate elements, skip it and call the function recursively with the remaining sub-lists.
  4. Return the result list.

Python3




def remove_duplicate_rows(test_list):
    if not test_list:
        return []
 
    # check if the first sub-list has duplicate elements
    if len(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
        return remove_duplicate_rows(test_list[1:])
 
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = remove_duplicate_rows(test_list)
 
# printing result
print("Rows after removal : " + str(res))
#This code is contributed by tvsk.


Output

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:

  1. Initialize an empty list to store the rows that don’t have duplicate elements.
  2. Use a list comprehension to iterate over each row of the input list.
  3. 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.
  4. Print the resulting list.

Below is the implementation of the above approach:

Python3




# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using all() function to check if all elements are unique
res = [sub for sub in test_list if all(sub.count(elem) == 1 for elem in sub)]
 
# printing result
print("Rows after removal : " + str(res))


Output

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.

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments