Python program to check if given value occurs atleast k times
Last Updated :
08 Apr, 2023
Given a list and some value (let’s say it N), write a Python program to check if the given value occurs atleast k-times in that list. We can use list comprehension to deal with this problem. We can add each occurrence of given value and check if it is greater than or equal to k. If the value returned is True, then set the flag to 1, else 0. Below is the Python implementation –
Python3
test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ]
print ("The original list is : " + str (test_list))
val = 5
k = 3
res = 0
res = sum ([ 1 for i in test_list if i = = val]) > = k
if res = = 1 : res = True
else : res = False
print (" % d occur atleast % d times ? :" % (val, k) + str (res))
|
Output:
The original list is : [1, 3, 5, 5, 4, 5]
5 occur atleast 3 times ? :True
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(1), as we only use a constant amount of extra space to store the value and the counter.
Method #2 : Using operator.countOf() method.
Python3
import operator as op
test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ]
print ( "The original list is : " + str (test_list))
val = 5
k = 3
res = op.countOf(test_list, val) > = k
if res = = 1 :
res = True
else :
res = False
print ( "%d occur atleast %d times ? :" % (val, k) + str (res))
|
Output
The original list is : [1, 3, 5, 5, 4, 5]
5 occur atleast 3 times ? :True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Using collections.Counter()
We can also use the collections.Counter() method to count the number of occurrences of each element in the list, and then check if the given value occurs at least k times.
Python3
import collections
test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ]
val = 5
k = 3
count = collections.Counter(test_list)
res = count[val] > = k
print ( "%d occur atleast %d times ? : %s" % (val, k, str (res)))
|
Output
5 occur atleast 3 times ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 4 : Using a loop to count the occurrences of the given value in the list and then comparing it with k.
steps to implement this method:
- Initialize a variable count to 0 to keep track of the number of occurrences of the given value in the list.
- Loop through the elements of the list using a for loop.
- For each element, check if it is equal to the given value. If it is, increment the count by 1.
- After the loop completes, check if the count is greater than or equal to k. If it is, set the variable res to True. Otherwise, set it to False.
- Print the result.
Python3
test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ]
val = 5
k = 3
count = 0
for num in test_list:
if num = = val:
count + = 1
res = count > = k
print ( "%d occur atleast %d times ? : %s" % (val, k, str (res)))
|
Output
5 occur atleast 3 times ? : True
Time complexity: O(n) – we need to loop through the entire list once.
Auxiliary space: O(1) – we only use a few variables to keep track of the count and the result, so the space complexity is constant.
Share your thoughts in the comments
Please Login to comment...