please provide the code as i am unable to do it
I am unable to solve this
hi @ashutoshu
i will provide you approach and algorithms of this problem
try to implement at your own
if find any difficulty you can ask
i will help you
Approach: The idea is to store the top k elements with maximum frequency. To store them a vector can be used. To keep the track of frequencies of elements create a HashMap to store element - frequency pair. Given a stream of numbers, when a new element appears in the stream update the frequency of that element in HashMap and put that element at the end of the vector of K numbers (total k + 1 elements) now compare adjacent elements of the list and swap if higher frequency element is stored next to it.
Algorithm:
1) Create a Hashmap and an vector of k + 1 length.
2) Traverse the input vector from start to end.
3) Insert the element at k + 1 th position of the vector, update the frequency of that element in HashMap.
4) Now, traverse the temp vector from start to end – 1
For very element, compare the frequency and swap if higher frequency element is stored next to it, if the frequency is same then swap is the next element is greater.
5) print the top k element in each traversal of original vector.