< Back

3005. Count Elements With Maximum Frequency

Given an array of integers, sum the counts of the integers that appear the most. Use the Counter
class from the collections package to count the occurrences of each integer. Calculate the max
count and sum the counts of the integers that have a count equal to the max count.

The solution is as follows:

  from collections import Counter

  class Solution:
      def maxFrequencyElements(self, nums: List[int]) -> int:
          counts = Counter(nums)
          max_freq = max(counts.values())
          return sum([counts[num] for num in counts if counts[num] == max_freq])

_ Time Complexity:

  O(n) - We iterate through the array to count all the integers.

_ Space Complexity:

  O(n) - We store the count of each interger.