Given N elements and the task is to implement a stack which removes and returns the maximum frequency element on every pop operation. If there’s a tie in the frequency then the topmost highest frequency element will be returned.
pop() -> returns 6, as 6 is the most frequent (frequency of 6 = 2 ).
pop() -> returns 8 (6 also has the highest frequency but it is not the topmost)
Approach: Maintaining two HashMap, one is frequency HashMap which maps elements to their frequencies and other is setMap which maps all the element with same frequency in one group (Stack).
FrequencyStack has 2 functions:
- push(int x): map the element (x) with frequency HashMap and update the maxfreq variable ( i.e. holds the maximum frequency till now ). setMap maintains a stack which contains all the elements with same frequency.
- pop(): First get the maxfreq element from setMap and then decrement the frequency of the popped element. After popping, if the stack becomes empty then decrement the maxfreq.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Design a stack with operations on middle element
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space
- Maximum element in an array which is equal to its frequency
- Tracking current Maximum Element in a Stack
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Replace each element by the difference of the total size of the array and frequency of that element
- Find maximum in stack in O(1) without using additional stack
- Sudo Placement[1.3] | Stack Design
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- Next Greater Frequency Element
- Find the element in a linked list with frequency at least N/3
- Check if frequency of each element in given array is unique or not
- Python | Element with largest frequency in list
- Find the element having different frequency than other array elements
- Frequency of each element of an array of small ranged values
- Sorting element of an array by frequency in decreasing order
- Smallest subarray having an element with frequency greater than that of other elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.