1
var FreqStack = function () {2
//hashMap to keep track of the values being repeated3
this.frequencyMap = {};5
//List map to keep track of the sequence of the value being entered8
//Max Frequency variable to keep track of the max frequency9
this.maxValueFrequency = 0;16
FreqStack.prototype.push = function (val) {17
//if the hashMap doesn't have value being pushed then make a entry to it with 1 else increament by 118
this.frequencyMap[val] = this.frequencyMap[val]19
? this.frequencyMap[val] + 122
//get the frequency of the value being pushed23
const currentValueFrequency = this.frequencyMap[val];25
//check if the current frequency is max or itself26
this.maxValueFrequency = Math.max(27
this.maxValueFrequency,31
//if current value frequency is not in the listmap then make a new entry else push it32
if (!this.listMap[currentValueFrequency])33
this.listMap[currentValueFrequency] = [val];34
else this.listMap[currentValueFrequency].push(val);40
FreqStack.prototype.pop = function () {41
//make a temporary list of the max value frequency42
const tempList = this.listMap[this.maxValueFrequency];44
//get the last element from the temporary list45
const top = tempList[tempList.length - 1];47
//remove the item from the list51
this.listMap[this.maxValueFrequency] = tempList;53
//if the popped item exist in the frequecy map then decrease it by 154
if (this.frequencyMap[top]) this.frequencyMap[top]--;56
//if the max value frequency in the listmap is blank then reduce the maxValueFrequency;57
if (this.listMap[this.maxValueFrequency].length === 0)58
this.maxValueFrequency--;60
//return the max value frequency with proper order if it is same64
//Time O(Log n) // for both Push and Pop65
//Space O(n) for storing all the values in the hashMap and listMap