Files
rose-ash/plans
giles be13f2daba
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
ocaml: phase 5.1 majority_vote.ml baseline (Boyer-Moore majority, [3;3;4;2;4;4;2;4;4] = 4)
O(n) time / O(1) space majority vote algorithm:

  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

The candidate is updated to the current element whenever count
reaches zero. When a strict majority exists, this guarantees the
result.

  majority [3;3;4;2;4;4;2;4;4] = 4   (5 of 9, > n/2)

97 baseline programs total.
2026-05-09 19:27:14 +00:00
..