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.
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
There are 3 occurrences of 0 in [0, 0, 0, 1, 2, 3, 3, 4, 4]
Method 2: Using Count
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.
Yes, 9 has 2 occurrence in [9, 8, 7, 6, 5, 9, 2]
- Python | Check if k occurs atleast n times in a list
- Python program to check if given value occurs atleast k times
- Python | Repeat each element K times in list
- Python - Test if K occurs N consecutive times
- Python | Check if any element in list satisfies a condition
- Python - Check if string starts with any element in list
- Python | Ways to check if element exists in list
- Python | Check if element exists in list of lists
- Find number of times every day occurs in a Year
- Python | Add similar value multiple times in list
- Python | Remove elements of list that are repeated less than k times
- Python program to search for the minimum element occuring consecutively n times in a matrix
- Python | Subtract two list elements if element in first list is greater
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.