C++ Implementation:
class FindMaxFreqNumber {
// adds a number to the sequence
struct comp{
bool operator()(const pair<int,int> &a, const pair<int,int> &b) const{
return b.second<a.second;
}
};
multiset<pair<int,int>,comp> st; //for returning max frequent and removing its instance when removeoldest is called.
unordered_map<int,int> mp; //for elements frequency
queue<int> q; //for oldest
void add(int num){
q.push(num);
mp[num]++; //update frequency
st.insert({num,mp[num]}); //insert new instance in the multiset
}
void removeOldest(){
if(q.size()==0) return; //if queue has no elements
int old= q.front(); q.pop(); //pop from queue
int freq= mp[old]--; //decrease frequency because we remove its one instance
st.erase(st.find({old,freq})); //remove first instance
}
int getMaxFreqNumber(){
pair<int,int> n= *(st.begin()); //top most is the max frequent
return n.first;
}
}