Given a list, the task is to find the number of occurrences of the largest element of the list.
Input : [1, 2, 8, 5, 8, 7, 8] Output :3 Input : [2, 9, 1, 3, 4, 5] Output :1
Method 1: The naive approach is to find the largest element present in the list using
max(list) function, then iterating through the list using a for loop and find the frequency of the largest element in the list. Below is the implementation.
Method 2: Using collections.Counter()
Once initialized, counters are accessed just like dictionaries. Also, it does not raise the KeyValue error (if key is not present) instead the value’s count is shown as 0.
Method 3: Using the dictionary
In this approach, the number of occurrences of each element is stored in a dictionary as a key-value pair, where key is the element and value is the frequency.
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course