Skip to main content

Count Elements With Maximum Frequency - Solution & Explanation

EasyArrayHash TableCounting10 min readAsked at: Amazon, Microsoft, Meta +5
Practice this problem

Problem Statement

You are given an array nums consisting of positive integers.

Return the total frequencies of elements in nums such that those elements all have the maximum frequency.

The frequency of an element is the number of occurrences of that element in the array.

 

Example 1:

Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.

Example 2:

Input: nums = [1,2,3,4,5]
Output: 5
Explanation: All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.

 

Constraints:

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

Approach Overview

Problem Overview: You receive an integer array and need to determine which values appear most frequently. Instead of returning the value itself, return the total number of occurrences of all elements that share the maximum frequency.

Approach 1: Iterative Traversal with Stack (O(n) time, O(n) space)

This method processes the array iteratively while using a stack to manage traversal order. Each element is pushed onto the stack and processed once. A frequency map (hash table) tracks how many times each number appears. While popping elements from the stack, update the count in the map and keep track of the current maximum frequency. After the traversal completes, iterate through the map and sum the counts of elements whose frequency equals the maximum.

The key idea is that every element is visited exactly once, and hash lookups provide constant-time updates. This keeps the overall complexity linear. The stack mainly acts as a structured traversal helper while the hash map performs the counting. This pattern appears frequently in problems involving Array traversal combined with Hash Table aggregation.

Approach 2: Recursive Method (O(n) time, O(n) space)

The recursive approach processes the array index by index. Each recursive call handles one element, updates its frequency in a hash map, and passes the map to the next call. During recursion, maintain a variable storing the maximum frequency seen so far. After reaching the base case (end of the array), iterate through the frequency map and sum all counts equal to the maximum frequency.

This approach demonstrates how counting logic can be expressed without explicit loops. The recursion stack replaces iterative traversal while the map stores frequencies. Time complexity remains O(n) because each element is processed once, though the recursion adds O(n) auxiliary stack usage. The main concept is still frequency tracking, a common pattern in Counting problems.

Recommended for interviews: Interviewers generally expect a direct counting solution using a hash map and a single pass through the array. The iterative approach demonstrates solid understanding of frequency maps and linear traversal. A recursive version works but adds unnecessary overhead, so the iterative counting pattern is typically preferred for clarity and efficiency.

Approach 1: Approach 1: Iterative Traversal with Stack

This approach uses an iterative method to traverse the structure utilizing a stack for efficient element retrieval and management. This reduces the overhead associated with recursion, like function call stack management.

This C program uses a stack data structure to store integers and perform basic stack operations such as push and pop. The stack is implemented using an array with helper functions to manage stack operations.

Code

C

C++

Complexity

Time Complexity: O(1) for both push and pop operations.
Space Complexity: O(n) where n is the capacity of the stack.

Try this approach in the editor →

Approach 2: Approach 2: Recursive Method

This approach utilizes a recursive method to process elements. It is intuitive and straightforward but needs careful consideration of recursive depth and stack limits in various environments.

This Java program computes the factorial of a number using a recursive method, demonstrating the straightforwardness and concise nature of recursion.

Code

Java

Python

Complexity

Time Complexity: O(n) where n is the input number.
Space Complexity: O(n) due to recursive call stack.

Try this approach in the editor →

Approach 3: Counting

We can use a hash table or array cnt to record the occurrence of each element.

Then we traverse cnt to find the element with the most occurrences, and let its occurrence be mx. We sum up the occurrences of elements that appear mx times, which is the answer.

The time complexity is O(n), and the space complexity is O(n). Where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Iterative Traversal with Stack

Time Complexity: O(1) for both push and pop operations.
Space Complexity: O(n) where n is the capacity of the stack.

Approach 2: Recursive Method

Time Complexity: O(n) where n is the input number.
Space Complexity: O(n) due to recursive call stack.

Counting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Traversal with StackO(n)O(n)General case when iterating through the array and building a frequency map using a hash table
Recursive MethodO(n)O(n)Useful for demonstrating recursion-based traversal or practicing recursion with counting logic

Video Solution

Count Elements With Maximum Frequency | One Pass | Two Pass | Leetcode 3005 • codestorywithMIK • 7,980 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Count Elements With Maximum Frequency easy or hard?
LeetCode classifies this problem as Easy. The main skill required is building and using a frequency map with a hash table, a common pattern in array and counting problems.
Count Elements With Maximum Frequency Python/Java solution
In Python, a dictionary or collections.Counter can track frequencies, while Java typically uses HashMap<Integer, Integer>. Both implementations iterate through the array, update counts, track the maximum frequency, and sum occurrences of elements with that maximum.
How to solve Count Elements With Maximum Frequency in O(n)?
Traverse the array and store frequencies in a hash map. Track the maximum frequency while updating counts. After processing all elements, sum the frequencies of values whose count equals the maximum frequency. This approach requires O(n) time and O(n) space.
What is the best approach for Count Elements With Maximum Frequency?
The most efficient approach uses a hash table to count the frequency of each element in one pass through the array. After building the frequency map, track the maximum frequency and sum the counts of all elements that match it. This runs in O(n) time and O(n) space.
Is Count Elements With Maximum Frequency asked at Google/Amazon/Meta?
Frequency counting problems are common in interviews at companies like Amazon, Google, and Meta because they test hash table usage and array traversal. Variations of this problem appear frequently in coding rounds and online assessments.
What data structure is used in Count Elements With Maximum Frequency?
A hash table (or dictionary/map) is the primary data structure used. It stores the frequency of each integer in the array and allows constant-time updates and lookups, making it ideal for counting-based problems.
What is the time complexity of Count Elements With Maximum Frequency?
The optimal solution runs in O(n) time because each element in the array is processed once while updating a frequency map. A second pass over the map to sum the maximum frequencies also takes linear time relative to unique elements, keeping the overall complexity O(n).

Ready to solve this problem?

Practice Count Elements With Maximum Frequency with our built-in code editor and test cases.

Practice on FleetCode