MatematicaInglese
Pubblicato in Math ∩ Programming
Autore Jeremy Kun

Problem: Given a massive data stream of $ n$ values in $ \ 1, 2, \dots, m \$ and the guarantee that one value occurs more than $ n/2$ times in the stream, determine exactly which value does so. Solution: (in Python) def majority(stream): held = next(stream) counter = 1 for item in stream: if item == held: counter += 1 elif counter == 0: held = item counter = 1 else: counter -= 1 return held Discussion: Let’s prove correctness.