Python | K occurrence element Test

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 a certain number of times in 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# K occurrence element Test# using sum() + list comprehension# initializing listtest_list = [1, 3, 5, 5, 4, 5]# printing original listprint("The original list is : " + str(test_list))# initializing kk = 3# initializing NN = 5# using sum() + list comprehension# K occurrence element Testres = 0res = sum([1 for i in test_list if i == N]) == kif res == 1: res = Trueelse: res = False# printing resultprint("Does N occur K times ? : " + str(res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does N occur K times ? : True
Time Complexity : O(n)
Auxiliary Space : O(n), where n is length of list.
Method #2: Using next() + islice()
These both functions can be used together to perform this particular task in a more efficient manner than above. The islice function would handle the summation part and the next function helps with iteration logic.
Python3
# Python3 code to demonstrate# K occurrence element Test# using next() + islice()from itertools import islice# initializing listtest_list = [1, 3, 5, 5, 4, 5]# printing original listprint("The original list is : " + str(test_list))# initializing kk = 3# initializing NN = 5# using next() + islice()# K occurrence element Testfunc = (True for i in test_list if i == N)res = next(islice(func, k - 1, None), False)# printing resultprint("Does N occur K times ? : " + str(res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does N occur K times ? : True
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The next() + islice() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is needed.
Method #3 : Using count() method
Python3
# Python3 code to demonstrate# K occurrence element Test# initializing listtest_list = [1, 3, 5, 5, 4, 5]# printing original listprint ("The original list is : " + str(test_list))# initializing kk = 3# initializing NN = 5res=Falseif(test_list.count(N)==k): res=True# printing resultprint ("Does N occur K times ? : " + str(res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does N occur K times ? : True
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required
Method #4 : Using operator.countOf() method
Approach
- Used operator.countOf() to count the occurrence of N in test_list
- Used if condition to check if that count is equal to k
- If the if condition is satisfied assign True to res
- Display the res variable
Python3
# Python3 code to demonstrate# K occurrence element Test# initializing listtest_list = [1, 3, 5, 5, 4, 5]# printing original listprint ("The original list is : " + str(test_list))# initializing kk = 3# initializing NN = 5res=Falseimport operatorif(operator.countOf(test_list,N)==k): res=True# printing resultprint ("Does N occur K times ? : " + str(res)) |
The original list is : [1, 3, 5, 5, 4, 5] Does N occur K times ? : True
Time Complexity : O(N)
Auxiliary Space : O(1)



