Watch 10 video solutions for Count Elements With Maximum Frequency, a easy level problem involving Array, Hash Table, Counting. This walkthrough by codestorywithMIK has 7,428 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |