Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Count Elements With Maximum Frequency

Count how many array elements belong to the value that appears most often.

Acceptance 0%
Problem Statement

Given an integer array, determine the highest frequency of any value in the array, then count how many elements belong to values that reach that same highest frequency. Return the total number of array elements contributed by all values tied for maximum frequency.

In other words, if several distinct numbers appear the same maximum number of times, add up the counts of all of those numbers.

Input Format

  • An integer array nums.
  • nums[i] is an integer value.

Output Format

  • Return a single integer: the total number of elements whose values have maximum frequency in nums.

Constraints

  • The array may contain duplicate values.
  • Treat all values independently by exact equality.
  • A linear-time counting approach is expected for interview practice.
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

4

Explanation

The frequencies are {1: 2, 2: 2, 3: 1, 4: 1}. The maximum frequency is 2, and two values reach it: 1 and 2. So the answer is 2 + 2 = 4.

Example 2

Input

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

Output

3

Explanation

The frequencies are {1: 2, 2: 3, 3: 1}. The maximum frequency is 3, so only value 2 contributes. The answer is 3.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.