Open In App

Python | Delete elements with frequency atmost K

Many methods can be employed to perform the deletion in the list. Be it the remove function, pop function, and many other functions. But most of the time, we usually don’t deal with the simple deletion, but with certain constraints. This article discusses certain ways in which we can delete only those elements which occur less than K times. 

Method #1: Using list comprehension + count() The idea applied here is to construct a new list using list comprehension and insert only those elements which occur more than K times. The count operation is done with the help of the count function. 




# Python3 code to demonstrate
# remove elements less than and equal K
# using list comprehension + count()
 
# initializing list
test_list = [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
# using list comprehension + count()
# remove elements less than K
res = [ i for i in test_list if test_list.count(i) > K]
 
# print result
print("The list removing elements less than and equal K  : " + str(res))

Output : 
The original list : [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
The list removing elements less than and equal K  : [3, 2, 3, 3, 2, 2, 2]

Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list. 

Method #2: Using Counter() + list comprehension This problem can be efficiently solved using the Counter function that precomputes the count of each element in list so that the decision to keep or reject a particular element takes lesser time.




# Python3 code to demonstrate
# remove elements less than and equal K
# using Counter() + list comprehension
from collections import Counter
 
# initializing list
test_list = [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
# using Counter() + list comprehension
# remove elements less than K
freq = Counter(test_list)
res = [ele for ele in test_list if freq[ele] > K]
 
# print result
print("The list removing elements less than and equal K  : " + str(res))

Output : 
The original list : [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
The list removing elements less than and equal K  : [3, 2, 3, 3, 2, 2, 2]

Method #3: Using operator.countOf() method




import operator as op
# Python3 code to demonstrate
# remove elements less than and equal K
# using list comprehension + operator.countOf()
 
# initializing list
test_list = [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
# using list comprehension + operator.countOf()
# remove elements less than K
res = [i for i in test_list if op.countOf(test_list, i) > K]
 
# print result
print("The list removing elements less than and equal K  : " + str(res))

Output
The original list : [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
The list removing elements less than and equal K  : [3, 2, 3, 3, 2, 2, 2]

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #4: Using numpy:

Algorithm:

  1. Initialize the input list and the threshold K.
  2. Use the reduce function from the functools module to count the frequency of each element in the list.
  3. Create a NumPy array from the input list.
  4. Create a boolean mask for the elements with frequency greater than K.
  5. Filter the elements from the NumPy array using the boolean mask.
  6. Convert the resulting NumPy array to a list using the tolist() method.
  7. Print the final list as the output.




from functools import reduce
import numpy as np
# initializing list
test_list = [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
K = 2
# printing original list
print("The original list : " + str(test_list))
  
# count the frequency of each element in the list
freq = reduce(lambda d, x: {**d, x: d.get(x, 0) + 1}, test_list, {})
 
# create a NumPy array from the list
arr = np.array(test_list)
 
# create a boolean mask for the elements with frequency > K
mask = np.array([freq[x] > K for x in arr])
 
# use the boolean mask to filter the elements
res = arr[mask]
 
 
print("The list removing elements less than and equal K  : " +str(res.tolist()))
#This code is contributed by Rayudu

Output:
The original list : [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
The list removing elements less than and equal K  : [3, 2, 3, 3, 2, 2, 2]

Time complexity:
The time complexity of this code is O(n), where n is the length of the input list. The reduce function takes O(n) time to count the frequency of each element in the list, and the creation of the boolean mask and filtering of the elements takes O(n) time as well. The conversion of the NumPy array to a list takes O(n) time

Auxiliary Space:
The space complexity of this code is O(n), where n is the length of the input list. The frequency dictionary created using the reduce function can take up to O(n) space in the worst case if all the elements in the list are unique. The NumPy array created from the list also takes O(n) space. The boolean mask and the filtered array take up additional O(n) space. Therefore, the overall space complexity is O(n).

Method #5: Using the heapq method:

Algorithm:

  1. Initialize an empty heap.
  2. Traverse through the input list and count the frequency of each element and check if the count of an element is greater than K. If yes, then add that element to the heap.
  3. After traversing the whole list, the heap will contain all elements with a frequency greater than K.
  4. Finally, extract all elements from the heap and add them to the result list.
  5. The resulting list will contain all elements with a frequency greater than K.

Below is the implementation of the above approach:




import heapq
 
# initializing list
test_list = [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
# using heapq method remove elements
# less than K
res = [i for i in test_list if test_list.count(i) > K]
heapq.heapify(res)
 
# print result
print("The list removing elements less than and equal K : " + str(res))
 
# This code is contributed by Jyothi pinjala.

Output
The original list : [1, 4, 3, 2, 3, 3, 2, 2, 2, 1]
The list removing elements less than and equal K : [2, 2, 2, 3, 2, 3, 3]

Time Complexity: The time complexity of this approach is O(n*log k), where n is the length of the input list and k is the size of the heap. The time complexity of counting the frequency of each element is O(n). The time complexity of adding an element to the heap is O(log K). Since we add each element to the heap only if its frequency is greater than K, the maximum size of the heap will be the number of unique elements with a frequency greater than K.

Space Complexity: The space complexity of this approach is O(k), where k is the maximum size of the heap. We need to store at most k elements in the heap.


Article Tags :