
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to construct Frequency Stack in C++
Suppose we want to construct one stack called FrequencyStack, Our FrequencyStack has two functions −
append(x), This will append or push a value x onto the stack.
pop(), This will remove and returns the most frequent element in the stack. If there are more than one elements with the same frequency, then the element closest to the top of the stack is removed and returned.
So, if the input is like append some elements like 7, 9, 7, 9, 6, 7, then perform the pop operations four times, then the output will be 7,9,7,6 respectively.
To solve this, we will follow these steps −
Define one map cnt
Define one map sts
maxFreq := 0
Define a function append(), this will take x,
(increase cnt[x] by 1)
maxFreq := maximum of maxFreq and cnt[x]
insert x into sts[cnt[x]]
Define a function pop()
maxKey := maxFreq
x := top element of sts[maxKey]
delete element from sts[maxKey]
if size of sts[maxKey] is same as 0, then −
delete maxKey from sts
(decrease maxFreq by 1)
(decrease cnt[x] by 1)
return x
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class FreqStack { public: unordered_map <int ,int > cnt; unordered_map <int, stack <int> >sts; int maxFreq = 0; FreqStack() { maxFreq = 0; cnt.clear(); sts.clear(); } void append(int x) { cnt[x]++; maxFreq = max(maxFreq, cnt[x]); sts[cnt[x]].push(x); } int pop() { int maxKey = maxFreq; int x = sts[maxKey].top(); sts[maxKey].pop(); if(sts[maxKey].size() == 0){ sts.erase(maxKey); maxFreq−−; } cnt[x]−−; return x; } }; main(){ FreqStack ob; ob.append(7); ob.append(9); ob.append(7); ob.append(9); ob.append(6); ob.append(7); cout << (ob.pop()) << endl; cout << (ob.pop()) << endl; cout << (ob.pop()) << endl; cout << (ob.pop()) << endl; }
Input
ob.append(7); ob.append(9); ob.append(7); ob.append(9); ob.append(6); ob.append(7); cout << (ob.pop()) << endl; cout << (ob.pop()) << endl; cout << (ob.pop()) << endl; cout << (ob.pop()) << endl;
Output
7 9 7 6
- Related Articles
- Program to construct Maximum Stack with given operations in C++
- Maximum Frequency Stack in C++
- C++ Program to Implement Stack
- C++ Program to Implement Stack in STL
- Corrupt stack problem in C, C++ program
- C++ Program to Implement Stack using array
- C++ Program to Implement Graph Structured Stack
- C++ Program to Implement Stack using linked list
- C++ Program to Implement Stack Using Two Queues
- C Program to find direction of growth of stack
- C++ program to construct graph with certain conditions
- Construct a BST from given postorder traversal using Stack in Python
- C# Program to Implement Stack with Push and Pop operations
- C++ Program to Construct Transitive Closure Using Warshall’s Algorithm
- Bands in Radio frequency Spectrum in C program
