Python | Check if k occurs atleast n times in a list
There are many common problems that a developer or common programmer comes across in day-day coding. One such problem is finding if an element occurs more than a certain number of times in the list. Let’s discuss certain ways to deal with this problem.
Method #1 : Using sum() + list comprehension The list comprehension can be clubbed with the sum function for achieving this particular task. The sum function does the summation part and the logical case of returning true is dealt in list comprehension part.
Python3
# Python3 code to demonstrate # check for minimum N occurrences of K # using sum() + list comprehension # initializing list test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ] # printing original list print ("The original list is : " + str (test_list)) # initializing k k = 5 # initializing N N = 3 # using sum() + list comprehension # checking occurrences of K atleast N times res = 0 res = sum ([ 1 for i in test_list if i = = k]) > = N if res = = 1 : res = True else : res = False # printing result print ("Does % d occur atleast % d times ? :" % (k, N) + str (res)) |
Method #2 : Using next() + islice() These both functions can be used together to perform this particular task in more efficient manner than above. The islice function would handle the summation part and next function helps with iteration logic.
Python3
# Python3 code to demonstrate # check for minimum N occurrences of K # using next() + islice() from itertools import islice # initializing list test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ] # printing original list print ("The original list is : " + str (test_list)) # initializing k k = 5 # initializing N N = 3 # using next() + islice() # checking occurrences of K atleast N times func = ( True for i in test_list if i = = k) res = next (islice(func, N - 1 , None ), False ) # printing result print ("Does % d occur atleast % d times ? :" % (k, N) + str (res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does 5 occur atleast 3 times ? : True
Method #3: Using count()
Python3
# Python3 code to demonstrate # check for minimum N occurrences of K # initializing list test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing k k = 5 # initializing N N = 3 # checking occurrences of K atleast N times res = False if (test_list.count(k)> = N): res = True # printing result print ( "Does %d occur atleast %d times ? : " % (k, N) + str (res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does 5 occur atleast 3 times ? : True
Method #4 : Using for loop
Python3
# Python3 code to demonstrate # check for minimum N occurrences of K # initializing list test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing k k = 5 # initializing N N = 3 # checking occurrences of K atleast N times res = False c = 0 for i in test_list: if (i = = k): c + = 1 if (c> = N): res = True # printing result print ( "Does %d occur atleast %d times ? : " % (k, N) + str (res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does 5 occur atleast 3 times ? : True
Method #5 : Using operator.countOf() method
Python3
# Python3 code to demonstrate # check for minimum N occurrences of K import operator as op # initializing list test_list = [ 1 , 3 , 5 , 5 , 4 , 5 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing k k = 5 # initializing N N = 3 # checking occurrences of K atleast N times res = False if (op.countOf(test_list, k) > = N): res = True # printing result print ( "Does %d occur atleast %d times ? : " % (k, N) + str (res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does 5 occur atleast 3 times ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...