Given List of elements, remove an element if it’s occurrence in list increases more than N.
Input : test_list = [6, 4, 6, 3, 6], N = 1
Output : [6, 4, 3]
Explanation : The occurrence 2nd onwards of 6 are removed.
Input : test_list = [6, 4, 6, 3, 6], N = 2
Output : [6, 4, 6, 3]
Explanation : The occurrence 3nd onwards of 6 are removed.
Method #1 : Using loop + count()
The combination of above functionalities can be used to solve this problem. In this, we perform iteration of elements and check if count is more than N of that element using count(), if yes, then we remove that element.
Python3
# Python3 code to demonstrate working of# Maximum N repeated Elements# Using loop + count() # initializing listtest_list = [5, 7, 7, 2, 5, 5, 7, 2, 2] # printing original listprint("The original list : " + str(test_list)) # initializing NN = 2 # Using loop + count()res = []for ele in test_list: # checking of elements occurrence is not greater than N if res.count(ele) < N: res.append(ele) # printing resultprint("Extracted elements : " + str(res)) |
The original list : [5, 7, 7, 2, 5, 5, 7, 2, 2] Extracted elements : [5, 7, 7, 2, 5, 2]
Method #2 : Using Counter() + loop
This is yet another way in which this task can be performed. In this, we use Counter() to perform counting of elements, and then append its size elements if less than N, if greater, list comprehension is used to extend elements till N. Does’nt preserve order.
Python3
# Python3 code to demonstrate working of# Maximum N repeated Elements# Using Counter() + loopfrom collections import Counter # initializing listtest_list = [5, 7, 7, 2, 5, 5, 7, 2, 2] # printing original listprint("The original list : " + str(test_list)) # initializing NN = 2 # Using Counter() + loopres = []temp = Counter(test_list)for key, ele in temp.items(): # Conditional check for size decision during append if ele <= N: res.extend([key for idx in range(ele)]) else: res.extend([key for idx in range(N)]) # printing resultprint("Extracted elements : " + str(res)) |
The original list : [5, 7, 7, 2, 5, 5, 7, 2, 2] Extracted elements : [5, 5, 7, 7, 2, 2]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


