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 <= 1001 <= nums[i] <= 100Problem 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.
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.
Time Complexity: O(1) for both push and pop operations.
Space Complexity: O(n) where n is the capacity of the stack.
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.
Time Complexity: O(n) where n is the input number.
Space Complexity: O(n) due to recursive call stack.
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.
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
| Approach | Complexity |
|---|---|
| Approach 1: Iterative Traversal with Stack | Time Complexity: O(1) for both push and pop operations. |
| Approach 2: Recursive Method | Time Complexity: O(n) where n is the input number. |
| Counting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Traversal with Stack | O(n) | O(n) | General case when iterating through the array and building a frequency map using a hash table |
| Recursive Method | O(n) | O(n) | Useful for demonstrating recursion-based traversal or practicing recursion with counting logic |
Count Elements With Maximum Frequency | One Pass | Two Pass | Leetcode 3005 • codestorywithMIK • 7,428 views views
Watch 9 more video solutions →Practice Count Elements With Maximum Frequency with our built-in code editor and test cases.
Practice on FleetCode