Python | Elements with Frequency equal K
Last Updated :
24 Mar, 2023
This is one of the most essential operation that programmer quite often comes in terms with. Be it development or competitive programming, this utility is quite essential to master as it helps to perform many tasks that involve this task to be its subtask. Lets discuss approach to achieve this operation.
Method #1 : Naive method As the brute force method, we just count all the elements and then just return the elements whose count equals K. This is the basic method that one could think of executing when faced with this issue.
Python3
# Python3 code to demonstrate
# Elements with Frequency equal K
# using naive method
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5]
# printing original list
print ("Original list : " + str(test_list))
# initializing K
K = 3
# using naive method to
# get most frequent element
res = []
for i in test_list:
freq = test_list.count(i)
if freq == K and i not in res:
res.append(i)
# printing result
print ("Elements with count K are : " + str(res))
Output : Original list : [9, 4, 5, 4, 4, 5, 9, 5]
Elements with count K are : [4, 5]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2: Using Counter() function
Python3
# Python3 code to demonstrate
# Elements with Frequency equal K
# using Counter() function
from collections import Counter
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5]
# printing original list
print("Original list : " + str(test_list))
# initializing K
K = 3
res = []
freq = Counter(test_list)
for key, value in freq.items():
if(value == K):
res.append(key)
# printing result
print("Elements with count K are : " + str(res))
OutputOriginal list : [9, 4, 5, 4, 4, 5, 9, 5]
Elements with count K are : [4, 5]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using Operator.countOf() function
Python3
# Python3 code to demonstrate
# Elements with Frequency equal K
import operator as op
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5]
# printing original list
print("Original list : " + str(test_list))
# initializing K
K = 3
res = []
unique_elements = set(test_list)
for i in unique_elements:
if op.countOf(test_list, i) == K:
res.append(i)
# printing result
print("Elements with count K are : " + str(res))
OutputOriginal list : [9, 4, 5, 4, 4, 5, 9, 5]
Elements with count K are : [4, 5]
Time Complexity:O(N)
Auxiliary Space:O(N)
Similar Reads
Python - Extract elements with equal frequency as value Given a list, extract all the elements having same frequency as its value. Examples: Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4, 4] Output : [1, 3, 4] Explanation : All elements occur equal times as their value. Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4] Output : [1, 3] Explanation :
6 min read
Python - Elements frequency in Tuple Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
7 min read
Python - Extract elements with Frequency greater than K Given a List, extract all elements whose frequency is greater than K. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8], K = 3 Output : [4, 3] Explanation : Both elements occur 4 times. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6], K = 2 Output : [4, 3, 6] Explanation : Occur 4, 3, and 3 time
7 min read
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 tho
7 min read
Sort List Elements by Frequency - Python Our task is to sort the list based on the frequency of each element. In this sorting process, elements that appear more frequently will be placed before those with lower frequency. For example, if we have: a = ["Aryan", "Harsh", "Aryan", "Kunal", "Harsh", "Aryan"] then the output should be: ['Aryan'
3 min read