Open In App
Related Articles

Python | K occurrence element Test

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 list
test_list = [1, 3, 5, 5, 4, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing k
k = 3
 
# initializing N
N = 5
 
# using sum() + list comprehension
# K occurrence element Test
res = 0
res = sum([1 for i in test_list if i == N]) == k
if res == 1:
    res = True
else:
    res = False
 
# printing result
print("Does N occur K times ? : " + str(res))

                    
Output : 
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 list
test_list = [1, 3, 5, 5, 4, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing k
k = 3
 
# initializing N
N = 5
 
# using next() + islice()
# K occurrence element Test
func = (True for i in test_list if i == N)
res = next(islice(func, k - 1, None), False)
 
# printing result
print("Does N occur K times ? : " + str(res))

                    
Output : 
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 list
test_list = [1, 3, 5, 5, 4, 5]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing k
k = 3
 
# initializing N
N = 5
res=False
if(test_list.count(N)==k):
    res=True
# printing result
print ("Does N occur K times ? : " + str(res))

                    

Output
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 

  1. Used operator.countOf() to count the occurrence of N in test_list
  2. Used if condition to check if that count is equal to k
  3. If the if condition is satisfied assign True to res
  4. Display the res variable

Python3

# Python3 code to demonstrate
# K occurrence element Test
 
# initializing list
test_list = [1, 3, 5, 5, 4, 5]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing k
k = 3
 
# initializing N
N = 5
res=False
import operator
if(operator.countOf(test_list,N)==k):
    res=True
# printing result
print ("Does N occur K times ? : " + str(res))

                    

Output
The original list is : [1, 3, 5, 5, 4, 5]
Does N occur K times ? : True

Time Complexity : O(N)

Auxiliary Space : O(1)



Last Updated : 24 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads