Related Articles
Design a stack which can give maximum frequency element
• Difficulty Level : Expert
• Last Updated : 28 Oct, 2019

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.

Examples:

Input:
push(4) 8
push(6) 6
push(7) 7
push(6) 6
push(8); 4

Output:
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)

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. 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.
2. 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:

## C++

 `// C++ implementation of the approach``#include``using` `namespace` `std;`` ` `// freqMap is to map element to its frequency``map<``int``, ``int``> freqMap;`` ` `// setMap is to map frequency to the``// element list with this frequency``map<``int``, stack<``int``> > setMap;`` ` `// Variable which stores maximum frequency``// of the stack element``int` `maxfreq = 0;`` ` `// Function to insert x in the stack``void` `push(``int` `x)``{`` ` `    ``// Frequency of x``    ``int` `freq = freqMap[x] + 1;`` ` `    ``// Mapping of x with its frequency``    ``freqMap[x]= freq;`` ` `    ``// Update maxfreq variable``    ``if` `(freq > maxfreq)``        ``maxfreq = freq;`` ` `    ``// Map element to its frequency list``    ``// If given frequency list doesn't exist``    ``// make a new list of the required frequency``    ``setMap[freq].push(x);``}`` ` `// Function to remove maximum frequency element``int` `pop()``{`` ` `    ``// Remove element from setMap``    ``// from maximum frequency list``    ``int` `top = setMap[maxfreq].top();``    ``setMap[maxfreq].pop();`` ` `    ``// Decrement the frequency of the popped element``    ``freqMap[top]--;`` ` `    ``// If whole list is poppped``    ``// then decrement the maxfreq``    ``if` `(setMap[maxfreq].size() == 0)``        ``maxfreq--;``    ``return` `top;``}`` ` `// Driver code``int` `main()``{`` ` `    ``// Push elements to the stack``    ``push(4);``    ``push(6);``    ``push(7);``    ``push(6);``    ``push(8);`` ` `    ``// Pop elements``    ``cout << (pop()) << ``"\n"` `;``    ``cout << (pop());``    ``return` `0;``}`` ` `// This code is contributed by Arnab Kundu`

## Java

 `// Java implementation of the approach``import` `java.util.*;`` ` `public` `class` `freqStack {`` ` `    ``// freqMap is to map element to its frequency``    ``static` `Map freqMap = ``new` `HashMap<>();`` ` `    ``// setMap is to map frequency to the``    ``// element list with this frequency``    ``static` `Map > setMap = ``new` `HashMap<>();`` ` `    ``// Variable which stores maximum frequency``    ``// of the stack element``    ``static` `int` `maxfreq = ``0``;`` ` `    ``// Function to insert x in the stack``    ``public` `static` `void` `push(``int` `x)``    ``{`` ` `        ``// Frequency of x``        ``int` `freq = freqMap.getOrDefault(x, ``0``) + ``1``;`` ` `        ``// Mapping of x with its frequency``        ``freqMap.put(x, freq);`` ` `        ``// Update maxfreq variable``        ``if` `(freq > maxfreq)``            ``maxfreq = freq;`` ` `        ``// Map element to its frequency list``        ``// If given frequency list doesn't exist``        ``// make a new list of the required frequency``        ``setMap.computeIfAbsent(freq, z -> ``new` `Stack()).push(x);``    ``}`` ` `    ``// Function to remove maximum frequency element``    ``public` `static` `int` `pop()``    ``{`` ` `        ``// Remove element from setMap``        ``// from maximum frequency list``        ``int` `top = setMap.get(maxfreq).pop();`` ` `        ``// Decrement the frequency of the popped element``        ``freqMap.put(top, freqMap.get(top) - ``1``);`` ` `        ``// If whole list is poppped``        ``// then decrement the maxfreq``        ``if` `(setMap.get(maxfreq).size() == ``0``)``            ``maxfreq--;``        ``return` `top;``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Push elements to the stack``        ``push(``4``);``        ``push(``6``);``        ``push(``7``);``        ``push(``6``);``        ``push(``8``);`` ` `        ``// Pop elements``        ``System.out.println(pop());``        ``System.out.println(pop());``    ``}``}`

## Python3

 `# Python3 implementation of the approach `` ` `# freqMap is to map element to its frequency ``freqMap ``=` `{}; `` ` `# setMap is to map frequency to the ``# element list with this frequency ``setMap ``=` `{}; `` ` `# Variable which stores maximum frequency ``# of the stack element ``maxfreq ``=` `0``; `` ` `# Function to insert x in the stack ``def` `push(x) :``    ``global` `maxfreq;``    ``if` `x ``not` `in` `freqMap :``        ``freqMap[x] ``=` `0``         ` `    ``# Frequency of x``    ``freq ``=` `freqMap[x] ``+` `1``;``     ` `    ``# Mapping of x with its Frequency``    ``freqMap[x]``=` `freq``     ` `    ``# Update maxfreq Variable``    ``if` `(freq > maxfreq) :``        ``maxfreq ``=` `freq``         ` `    ``# Map element to its frequency list``    ``# If given frequency list doesn't exist``    ``# make a new list of the required frequency``    ``if` `freq ``not` `in` `setMap :``        ``setMap[freq] ``=` `[]``     ` `    ``setMap[freq].append(x); `` ` `# Function to remove maximum frequency element ``def` `pop() :`` ` `    ``global` `maxfreq``     ` `    ``# Remove element from setMap``    ``# from maximum frequency list``    ``top ``=` `setMap[maxfreq][``-``1``];``    ``setMap[maxfreq].pop();``     ` `    ``# Decrement the frequency``    ``# of the popped element``    ``freqMap[top] ``-``=` `1``;``     ` `    ``# If whole list is poppped``    ``# then decrement the maxfreq``    ``if` `(``len``(setMap[maxfreq]) ``=``=` `0``) :``        ``maxfreq ``-``=` `1``;``         ` `    ``return` `top; `` ` `# Driver code ``if` `__name__ ``=``=` `"__main__"` `: `` ` `    ``# Push elements to the stack ``    ``push(``4``); ``    ``push(``6``); ``    ``push(``7``); ``    ``push(``6``); ``    ``push(``8``); `` ` `    ``# Pop elements ``    ``print``(pop()) ; ``    ``print``(pop()); `` ` `# This code is contributed by AnkitRai01 `
Output:
```6
8
```

My Personal Notes arrow_drop_up