let majority xs =
  let cand = ref 0 in
  let count = ref 0 in
  List.iter (fun x ->
    if !count = 0 then begin
      cand := x;
      count := 1
    end else if x = !cand then count := !count + 1
    else count := !count - 1
  ) xs;
  !cand

;;

majority [3; 3; 4; 2; 4; 4; 2; 4; 4]
