Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created September 22, 2025 21:57
Show Gist options
  • Select an option

  • Save Ifihan/7018556fa607105aa8a3529bf1c48781 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/7018556fa607105aa8a3529bf1c48781 to your computer and use it in GitHub Desktop.
Count Elements With Maximum Frequency

Question

Approach

I count how many times each number appears in the array using a frequency counter (like collections.Counter). Then I find the maximum frequency among all elements. Finally, I add up the frequencies of all elements that appear with this maximum frequency. This gives the total number of elements in the array that have the maximum frequency.

Implementation

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

Complexities

  • Time: O(n)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment