Frequency stack

Write codeHard35 min

Your task

Implement a FreqStack: push(x) adds a value; pop() removes and returns the most frequent value, breaking ties by the one pushed most recently (closest to top). Simulate a sequence of operations: each op is ['push', x] or ['pop']. Return the list of values returned by each 'pop'.

Examples

freq_stack(ops: list[list]) -> list[int]
Example 1
Inputfreq_stack([["push", 5], ["push", 7], ["push", 5], ["push", 7], ["push", 4], ["push", 5], ["pop"], ["pop"], ["pop"], ["pop"]])
Output[5, 7, 5, 4]