A diagnostics unit tracks a multiset of fault codes (integers). Starting empty, it processes q operations, each one of:
ADD x — add one occurrence of code x.REMOVE x — remove one occurrence of code x. It is guaranteed at least one occurrence exists.MODE — report the code with the highest current occurrence count. If several codes tie for the highest count, report the smallest such code. It is guaranteed the multiset is non-empty.Line 1: an integer q.
Next q lines: ADD x, REMOVE x, or MODE.
For every MODE operation, in order, print the reported code on its own line.
REMOVE targets a present code; every MODE sees a non-empty multiset.Example 1
Input
6 ADD 5 ADD 5 ADD 3 MODE ADD 3 MODE
Expected
5 3
Explanation
Counts are {5:2, 3:1}; MODE prints 5. After another 3, counts are {5:2, 3:2}; the tie is broken by the smaller code, so MODE prints 3.
Example 2
Input
5 ADD 7 ADD 2 MODE REMOVE 7 MODE
Expected
2 2
Explanation
Counts are {7:1, 2:1}; the tie goes to the smaller code, so MODE prints 2. After removing 7, only 2 remains, so MODE prints 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →