Magicsheet logo

Count Elements With Maximum Frequency

Easy
79.2%
Updated 6/1/2025

Count Elements With Maximum Frequency

What is this problem about?

The Count Elements With Maximum Frequency interview question asks you to find the total number of elements in an array that have the highest frequency. For example, in [1, 2, 2, 3, 3], the maximum frequency is 2 (for elements 2 and 3). Since there are two such elements and each appears twice, the total count is 4.

Why is this asked in interviews?

Companies like Microsoft and Google use the Hash Table interview pattern for this problem as a quick check of your data manipulation skills. It’s an "Easy" difficulty task that requires a two-step logic: first find the max frequency, then sum up the counts of all elements that match it. It evaluates basic array traversal and frequency mapping.

Algorithmic pattern used

The problem uses Frequency Counting.

  1. Use a Hash Map to count the frequency of each element in the array.
  2. Find the maximum value among all frequencies.
  3. Iterate through the map again and sum up the frequencies that are equal to the maximum frequency.

Example explanation

nums = [1, 2, 2, 3, 1, 4]

  1. Frequencies: {1: 2, 2: 2, 3: 1, 4: 1}.
  2. Max Frequency: 2.
  3. Elements with frequency 2: 1 and 2.
  4. Sum of their frequencies: 2 + 2 = 4. Result: 4.

Common mistakes candidates make

  • Returning the Max Frequency: Returning "2" instead of the total count of elements "4".
  • One-pass Logic Error: Trying to calculate everything in one pass without correctly updating the running total when a new maximum frequency is discovered.
  • Sorting: Using sorting (O(N log N)) when a Hash Map (O(N)) is more efficient.

Interview preparation tip

While you can do this in one pass, it's often cleaner to do it in two steps. In a one-pass approach, if you find a new max_freq, you have to reset your total_count to that new frequency.

Similar Questions