Python | Check if any element occurs n times in given list

Given a list, the task is to find whether any element occurs ‘n’ times in given list of integers. It will basically check for the first element that occurs n number of times.

Examples:

Input: l =  [1, 2, 3, 4, 0, 4, 3, 2, 1, 2], n = 3
Output :  2

Input: l =  [1, 2, 3, 4, 0, 4, 3, 2, 1, 2, 1, 1], n = 4
Output :  1

Below are some methods to do the task in Python –



Method 1: Using Simple Iteration and Sort

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to find occurrence of any element 
# appearing 'n' times
  
# Input Initialisation
input = [1, 2, 3, 0, 4, 3, 4, 0, 0]
  
# Sort Input
input.sort()
  
# Constants Declaration
n = 3
prev = -1
count = 0
flag = 0
  
# Iterating
for item in input:
    if item == prev:
        count = count + 1
    else:
        count = 1
    prev = item
      
    if count == n:
        flag = 1
        print("There are {} occurrences of {} in {}".format(n, item, input))
        break
  
# If no element is not found.
if flag == 0:
    print("No occurrences found")

chevron_right


Output :

There are 3 occurrences of 0 in [0, 0, 0, 1, 2, 3, 3, 4, 4]

 
Method 2: Using Count

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to find occurrence of any element 
# appearing 'n' times
  
# Input list initialisation
input = [1, 2, 3, 4, 0, 4, 3, 4]
  
# Constant declaration
n = 3
  
# print
print("There are {} occurrences of {} in {}".format(n, input.count(n), input))

chevron_right


Output :

There are 3 occurrences of 2 in [1, 2, 3, 4, 0, 4, 3, 4]

 
Method 3: Using defaultdict

We first populate item of list in a dictionary and then we find whether count of any element is equal to n.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to find occurrence of any element 
# appearing 'n' times
  
# importing
from collections import defaultdict
  
# Dictionary declaration
dic = defaultdict(int)
  
# Input list initialisation
Input = [9, 8, 7, 6, 5, 9, 2]
  
# Dictionary populate
for i in Input:
    dic[i]+= 1
  
# constant declaration
n = 2
flag = 0
  
# Finding from dictionary
for element in Input:
    if element in dic.keys() and dic[element] == 2:
        print("Yes, {} has {} occurrence in {}.".format(element, n, Input))
        flag = 1
        break
  
# if element not found.
if flag == 0:
    print("No occurrences found")

chevron_right


Output :

Yes, 9 has 2 occurrence in [9, 8, 7, 6, 5, 9, 2]



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.