Count Occurrences of the Most Frequent Values

Quick Overview

Count all occurrences of values tied for the greatest frequency in a positive integer array, with clear tie handling and complete written reasoning.

Count Occurrences of the Most Frequent Values

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Count Occurrences of the Most Frequent Values You are given an array `nums` of positive integers. A value's frequency is the number of times it occurs in the array. Find the greatest frequency of any value, then return the total number of array elements whose values have that frequency. If several values tie for the greatest frequency, include all of their occurrences. ### Examples **Example 1** ```text Input: nums = [5,5,5,8,8,8,1,1] Output: 6 ``` Values `5` and `8` each occur three times, while `1` occurs twice. Both maximum-frequency values contribute all three of their occurrences, giving `3 + 3 = 6`. **Example 2** ```text Input: nums = [3,3,3,3,6,6] Output: 4 ``` Only `3` has the greatest frequency, which is four. Its four occurrences contribute to the answer; the two occurrences of `6` do not. ### Constraints - `1 <= nums.length <= 100`. - `1 <= nums[i] <= 100`. ### Clarifying Questions - Is the result the number of distinct values tied for the greatest frequency? No; count their total occurrences in the array. - Should only one value be selected when several tie? No; include every value with the greatest frequency. - If all values occur once, what is counted? Every element, because all values tie with frequency one. - Does the order of equal values matter? No; the answer depends on their frequencies. ### C++ Interface ```cpp class Solution { public: int maxFrequencyElements(vector<int>& nums) { } }; ``` ### Reasoning Checkpoints - Distinguish a value's frequency from the total number of qualifying occurrences. - Include every value tied for the greatest frequency. - Justify the time and space costs under the stated bounds. ```hint Count values before choosing the qualifying frequencies After counting occurrences of each value, identify the greatest count. Add every count equal to that greatest count, rather than adding one per qualifying value. ```

Overview: Count all occurrences of values tied for the greatest frequency in a positive integer array, with clear tie handling and complete written reasoning.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Count Occurrences of the Most Frequent Values

You are given an array nums of positive integers. A value's frequency is the number of times it occurs in the array.

Find the greatest frequency of any value, then return the total number of array elements whose values have that frequency. If several values tie for the greatest frequency, include all of their occurrences.

Examples

Example 1

Input: nums = [5,5,5,8,8,8,1,1]
Output: 6

Values 5 and 8 each occur three times, while 1 occurs twice. Both maximum-frequency values contribute all three of their occurrences, giving 3 + 3 = 6.

Example 2

Input: nums = [3,3,3,3,6,6]
Output: 4

Only 3 has the greatest frequency, which is four. Its four occurrences contribute to the answer; the two occurrences of 6 do not.

Constraints

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

Clarifying Questions Guidance

  • Is the result the number of distinct values tied for the greatest frequency? No; count their total occurrences in the array.
  • Should only one value be selected when several tie? No; include every value with the greatest frequency.
  • If all values occur once, what is counted? Every element, because all values tie with frequency one.
  • Does the order of equal values matter? No; the answer depends on their frequencies.

C++ Interface

class Solution {
public:
    int maxFrequencyElements(vector<int>& nums) {
        
    }
};

Reasoning Checkpoints

  • Distinguish a value's frequency from the total number of qualifying occurrences.
  • Include every value tied for the greatest frequency.
  • Justify the time and space costs under the stated bounds.