Count Elements With Maximum Frequency

Easy
591.

Count Elements With Maximum Frequency

Count Elements With Maximum Frequency is an easy coding problem on arrays, hash table and counting, asked in Amazon, TCS and CRED interviews.

Given an array of positive integers nums, return the total number of elements that have the maximum frequency.

If several values tie for the highest frequency, every one of their occurrences is counted.

Example 1

Input
nums = [1,2,2,3,1,4]
Output
4
Explanation
1 and 2 both occur twice, contributing 2 + 2 = 4 elements.

Example 2

Input
nums = [1,2,3,4,5]
Output
5
Explanation
Every value occurs once, so all five count.

Example 3

Input
nums = [7,7,7]
Output
3

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxFrequencyElements = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Count Elements With Maximum Frequency — Problem & Solution

Count Elements With Maximum Frequency solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Count Elements With Maximum Frequency is an easy coding problem on arrays, hash table and counting, asked in Amazon, TCS and CRED interviews.

  • Difficulty: Easy
  • Topics: Arrays, Hash Table, Counting
  • Asked at: Amazon, TCS, CRED
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given an array of positive integers nums, return the total number of elements that have the maximum frequency.

If several values tie for the highest frequency, every one of their occurrences is counted.

Example 1

Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: 1 and 2 both occur twice, contributing 2 + 2 = 4 elements.

Example 2

Input: nums = [1,2,3,4,5]
Output: 5
Explanation: Every value occurs once, so all five count.

Example 3

Input: nums = [7,7,7]
Output: 3

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Hints for Count Elements With Maximum Frequency

  1. Hint 1

    Count the frequencies, then find the maximum.

  2. Hint 2

    Sum that maximum once for every value that attains it.

  3. Hint 3

    Equivalently: maxFreq × (number of values with maxFreq).

How to solve Count Elements With Maximum Frequency

Two sweeps over the frequency table — one to find the peak, one to add up every value that reaches it.

Approach

  1. Tally the occurrences of each value.
  2. Find the largest tally.
  3. Add that tally once per value achieving it.

Why it works

The question counts elements, not distinct values, so each tying value contributes its whole frequency rather than 1.

Complexity

  • Time — O(n)
  • Space — O(V)

Pitfalls

  • Counting the number of tying values instead of their total elements answers 2 rather than 4 on the first example.
  • Finding the max and summing in one pass is possible but easy to get wrong when the max changes mid-sweep.

Reference solution

Python

from typing import List

def maxFrequencyElements(nums: List[int]) -> int:
    count = {}
    for x in nums:
        count[x] = count.get(x, 0) + 1
    best = max(count.values())
    return sum(c for c in count.values() if c == best)

Java

public static int maxFrequencyElements(int[] nums) {
    int[] count = new int[101];
    for (int x : nums) count[x]++;
    int best = 0;
    for (int v = 1; v <= 100; v++) if (count[v] > best) best = count[v];
    int total = 0;
    for (int v = 1; v <= 100; v++) if (count[v] == best) total += best;
    return total;
}

C++

int maxFrequencyElements(vector<int>& nums) {
    vector<int> count(101, 0);
    for (int x : nums) count[x]++;
    int best = 0;
    for (int v = 1; v <= 100; v++) if (count[v] > best) best = count[v];
    int total = 0;
    for (int v = 1; v <= 100; v++) if (count[v] == best) total += best;
    return total;
}

JavaScript

var maxFrequencyElements = function(nums) {
    const count = new Array(101).fill(0);
    for (let i = 0; i < nums.length; i++) count[nums[i]]++;
    let best = 0;
    for (let v = 1; v <= 100; v++) if (count[v] > best) best = count[v];
    let total = 0;
    for (let v = 1; v <= 100; v++) if (count[v] === best) total += best;
    return total;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Hashing: Hash Maps and Hash Sets