895. Maximum Frequency Stack LeetCode Solution
In this guide, you will get 895. Maximum Frequency Stack LeetCode Solution with the best time and space complexity. The solution to Maximum Frequency Stack problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Frequency Stack solution in C++
- Maximum Frequency Stack solution in Java
- Maximum Frequency Stack solution in Python
- Additional Resources

Problem Statement of Maximum Frequency Stack
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the FreqStack class:
FreqStack() constructs an empty frequency stack.
void push(int val) pushes an integer val onto the top of the stack.
int pop() removes and returns the most frequent element in the stack.
If there is a tie for the most frequent element, the element closest to the stack’s top is removed and returned.
Example 1:
Input
[“FreqStack”, “push”, “push”, “push”, “push”, “push”, “push”, “pop”, “pop”, “pop”, “pop”]
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
Output
[null, null, null, null, null, null, null, 5, 7, 5, 4]
Explanation
FreqStack freqStack = new FreqStack();
freqStack.push(5); // The stack is [5]
freqStack.push(7); // The stack is [5,7]
freqStack.push(5); // The stack is [5,7,5]
freqStack.push(7); // The stack is [5,7,5,7]
freqStack.push(4); // The stack is [5,7,5,7,4]
freqStack.push(5); // The stack is [5,7,5,7,4,5]
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].
Constraints:
0 <= val <= 109
At most 2 * 104 calls will be made to push and pop.
It is guaranteed that there will be at least one element in the stack before calling pop.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(|\texttt{push()}| – O(|\texttt{pop()}|)
895. Maximum Frequency Stack LeetCode Solution in C++
class FreqStack {
public:
void push(int val) {
countToStack[++count[val]].push(val);
maxFreq = max(maxFreq, count[val]);
}
int pop() {
const int val = countToStack[maxFreq].top();
countToStack[maxFreq].pop();
--count[val];
if (countToStack[maxFreq].empty())
--maxFreq;
return val;
}
private:
int maxFreq = 0;
unordered_map<int, int> count;
unordered_map<int, stack<int>> countToStack;
};
/* code provided by PROGIEZ */
895. Maximum Frequency Stack LeetCode Solution in Java
class FreqStack {
public void push(int val) {
count.merge(val, 1, Integer::sum);
countToStack.putIfAbsent(count.get(val), new ArrayDeque<>());
countToStack.get(count.get(val)).push(val);
maxFreq = Math.max(maxFreq, count.get(val));
}
public int pop() {
final int val = countToStack.get(maxFreq).pop();
count.merge(val, -1, Integer::sum);
if (countToStack.get(maxFreq).isEmpty())
--maxFreq;
return val;
}
private int maxFreq = 0;
private Map<Integer, Integer> count = new HashMap<>();
private Map<Integer, Deque<Integer>> countToStack = new HashMap<>();
}
// code provided by PROGIEZ
895. Maximum Frequency Stack LeetCode Solution in Python
class FreqStack:
def __init__(self):
self.maxFreq = 0
self.count = collections.Counter()
self.countToStack = collections.defaultdict(list)
def push(self, val: int) -> None:
self.count[val] += 1
self.countToStack[self.count[val]].append(val)
self.maxFreq = max(self.maxFreq, self.count[val])
def pop(self) -> int:
val = self.countToStack[self.maxFreq].pop()
self.count[val] -= 1
if not self.countToStack[self.maxFreq]:
self.maxFreq -= 1
return val
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.