![]() |
VOOZH | about |
Sometimes, while working with records, we can have a problem of removing those records which occur more than once. This kind of application can occur in web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + set() + count() Initial approach that can be applied is that we can iterate on each tuple and check it’s count in list using count(), if greater than one, we can remove them, converting then to set.
Step-by-step approach :
Below is the implementation of the above approach:
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)] All the non Duplicate from list are : [(6, 7), (3, 6)]
Time complexity: O(n^2), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.
Method #2 : Using Counter() + items() + list comprehension The combination of above functions can also be used to perform this particular task. In this, we just get the count of each occurrence of element using Counter() as dictionary and then extract all those whose value is equal to 1.
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)] All the non Duplicate from list are : [(6, 7), (3, 6)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method #3: Using operator.countOf() method
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)] All the non Duplicate from list are : [(6, 7), (3, 6)]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using a dictionary to track occurrences
This method uses a dictionary to track the occurrences of each element in the list. We iterate through the list and increment the count for each element in the dictionary. Then we use a list comprehension to extract all elements with count 1, which are the non-duplicate elements.
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)] All the non Duplicate from list are : [(3, 6), (6, 7)]
Time complexity: O(n) because it iterates through the list once to create the dictionary, and then once more to extract the non-duplicate elements using a list comprehension.
Auxiliary space: O(n) because it uses a dictionary to track the occurrences of each element in the list, which can potentially store all the elements of the list.
Method #5: Using a loop and a set to track occurrences
Iterate through the list and use a dictionary to keep track of the items that have already appeared. If an item has not appeared before, we add it to the output list.
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)] All the non Duplicate from list are : [(3, 6), (6, 7)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), since we use a dictionary to keep track of occurrences.
Method 6: Using list.count() method and list slicing to find non-duplicate items
The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)] All the non Duplicate from list are : [(3, 6), (6, 7)]
Time complexity: O(n^2)
Auxiliary space: O(n)