You are given numbers one at a time. After each number, your task is to report the mode (the most frequent number) among the numbers seen so far. If there are multiple modes, report the smallest of them.
You may assume that each number is an integer in the range 1 \dots 10^9 and that there are at most 10^5 numbers in total.
In a file mode.py
, implement a class Mode
with a function add
that adds a new number and returns the resulting mode.
class Mode: def __init__(self): # TODO def add(self, x): # TODO if __name__ == "__main__": m = Mode() print(m.add(1)) # 1 print(m.add(2)) # 1 print(m.add(2)) # 2 print(m.add(1)) # 1 print(m.add(3)) # 1 print(m.add(3)) # 1 print(m.add(3)) # 3