Skip to content
Snippets Groups Projects
Select Git revision
  • ae052145fff253396cb4651c811e2d5e3ef1056f
  • master default protected
  • debug-partition-size
  • wta-generator
  • fixes
  • bench-hex
  • ci-artifacts
  • new-monoids
  • stack
  • sumbag
  • tutorial
  • web
  • features/disable-sanity
  • ghc-8.4.4
  • linux-bin-artifacts
  • syntax-doc
  • ci-stack
  • rationals
  • double-round
  • init-time
  • group-weight
21 results

PossibleMajorityCandidate.hs

Blame
  • PossibleMajorityCandidate.hs 609 B
    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    
    module Data.Algorithm.PossibleMajorityCandidate where
    
    import Data.Foldable
    
    -- | Compute the possible majority candidate in t
    --
    -- The result is undefined if t is empty
    --
    -- Runtime is O(n)
    possibleMajorityCandidate :: forall a t. (Eq a, Foldable t) => t a -> a
    possibleMajorityCandidate = snd . foldl' loop (0, undefined)
      where
        loop :: (Int, a) -> a -> (Int, a)
        loop (!count,pmc) !a
          | count == 0  = (1, a)
          | a == pmc    = (count+1, pmc)
          | otherwise   = (count-1, pmc)
    {-# INLINE possibleMajorityCandidate #-}