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 timestest_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 occurrencesres = 0res = sum([1 for i in test_list if i == val]) >= k if res == 1 : res = Trueelse : 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 timesimport operator as optest_list = [1, 3, 5, 5, 4, 5]# printing original listprint("The original list is : " + str(test_list))# value to be checkedval = 5# initializing kk = 3res = op.countOf(test_list, val) >= kif res == 1: res = Trueelse: res = False# printing resultprint("%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 collectionstest_list = [1, 3, 5, 5, 4, 5]# value to be checkedval = 5# initializing kk = 3count = collections.Counter(test_list)res = count[val] >= k# printing resultprint("%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 checkedval = 5# initializing kk = 3count = 0for num in test_list: if num == val: count += 1res = count >= k# printing resultprint("%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.
