Implement the class FastMode
with the following methods:
add(x, k)
: add the numberx
to the listk
timesmode()
: return the mode of the list, i.e., the most frequent element (if there are multiple modes, return the smallest of them)
The time complexity of both methods should be O(1).
In a file fastmode.py
, implement a class FastMode
according to the following template:
class FastMode: def __init__(self): # TODO def add(self, x, k): # TODO def mode(self): # TODO if __name__ == "__main__": m = FastMode() m.add(4, 7) print(m.mode()) # 4 m.add(8, 5) print(m.mode()) # 4 m.add(8, 3) print(m.mode()) # 8 m.add(4, 1) print(m.mode()) # 4