# LeetCode: Maximum Frequency Stack Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var FreqStack = function () {2  this.freqMap = new Map()3  this.freqWithElsArr = []4  this.mostFreq = 05}6
7/**8 * @param {number} x9 * @return {void}10 */11FreqStack.prototype.push = function (x) {12  const freq = (this.freqMap.get(x) || 0) + 113  if (!this.freqWithElsArr[freq]) {14    this.freqWithElsArr[freq] = []15  }16
17  this.freqWithElsArr[freq].push(x)18  this.freqMap.set(x, freq)19  this.mostFreq = Math.max(this.mostFreq, freq)20
21  return null22}23
24/**25 * @return {number}26 */27FreqStack.prototype.pop = function () {28  const x = this.freqWithElsArr[this.mostFreq].pop()29
30  if (this.freqWithElsArr[this.mostFreq].length === 0) {31    this.mostFreq--32  }33
34  this.freqMap.set(x, this.freqMap.get(x) - 1)35
36  return x37}38
39/**40 * Your FreqStack object will be instantiated and called as such:41 * var obj = new FreqStack()42 * obj.push(x)43 * var param_2 = obj.pop()44 */```

leetcode

hash table

stack

## Next Post

LeetCode: Set Mismatch

Mar 2, 2021

Search Posts