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
# Python program to check if given # value occurs atleast k times test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ] # printing original list print ("The original list is : " + str (test_list)) # value to be checked val = 5 # initializing k k = 3 # using sum() + list comprehension # checking occurrences res = 0 res = sum ([ 1 for i in test_list if i = = val]) > = k if res = = 1 : res = True else : res = False # printing result print (" % d occur atleast % d times ? :" % (val, k) + str (res)) |
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
# Python program to check if given # value occurs atleast k times import operator as op test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ] # printing original list print ( "The original list is : " + str (test_list)) # value to be checked val = 5 # initializing k k = 3 res = op.countOf(test_list, val) > = k if res = = 1 : res = True else : res = False # printing result print ( "%d occur atleast %d times ? :" % (val, k) + str (res)) |
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 ] # value to be checked val = 5 # initializing k k = 3 count = collections.Counter(test_list) res = count[val] > = k # printing result print ( "%d occur atleast %d times ? : %s" % (val, k, str (res))) |
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 ] # value to be checked val = 5 # initializing k k = 3 count = 0 for num in test_list: if num = = val: count + = 1 res = count > = k # printing result print ( "%d occur atleast %d times ? : %s" % (val, k, str (res))) |
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.