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 2 occurrences of 3 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]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Check if k occurs atleast n times in a list
- Python program to check if given value occurs atleast k times
- Python - Test if K occurs N consecutive times
- Find number of times every day occurs in a Year
- Python | Check if any element in list satisfies a condition
- Python - Check if string starts with any element in list
- Python - Check if any list element is present in Tuple
- Python | Repeat each element K times in list
- Python - Test if Substring occurs in specific position
- Python | Check if suffix matches with any string in given list
- Python | Check if string ends with any string in given list
- Python | Test if any list element returns true for condition
- Python | Check if two lists have any element in common
- Python | Check if any String is empty in list
- Python program to search for the minimum element occurring consecutively n times in a matrix
- Python | Add similar value multiple times in list
- Python | Remove elements of list that are repeated less than k times
- Python | Check if a list exists in given list of lists
- PyQt5 - Selecting any one check box among group of check boxes
- Python | Slicing list from Kth element to last element
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.