Top k most frequent number in a stream

please explain the sample test case in detail

hi @dhairya16 please see this thread

1
5 2
5 1 3 5 2

5 comes : print( { 5 } )
1 comes : print( { 1,5 } )
3 comes : print( { 1,3 } ) , all three have same frequency so taking smaller 2.
5 comes : print( { 5,1 } ) , now 5 has max frequency.
2 comes : print( { 5,1 } )

Final output : 5, 1 5, 1 3, 5 1, 5 1.

in this when 3 is included then why output is not {1, 5}
as this is given in question
“We basically need to print top k numbers sorted by frequency when input stream has included k distinct elements”
so the topmost 2 elements will be 5 & 1
correct me if i did any mistake in understanding

@dhairya16 topmost means the frequency… it has been explained above that if frequency of all elements is same then you have to consider the smaller numbers

when 5 second time comes then why {5, 3} is not output

@dhairya16 5 will be printed because it has frequency 2. Rest all elements have frequency 1, so the smallest element is printed, ie 1.

please give hint, i am unable to think approach

@dhairya16 please refer to this article

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.