majority ( seq -- elt/f ) (original) (raw)

majority ( seq -- elt/f )

Vocabulary
math.extras

Inputs

seq a sequence

Outputs

elt/f an object

Word description
Returns the element of seq that is in the majority, provided there is such an element, using a linear-time majority vote algorithm.

Definition

USING: kernel math sequences ;

IN: math.extras

: majority ( seq -- elt/f )
[ f 0 ] dip
[ over zero? [ 2nip 1 ] [ pick = [ 1 + ] [ 1 - ] if ] if ]
each zero? [ drop f ] when ;