[Haskell-beginners] Re: Majority Element

Keith Sheppard keithshep at gmail.com
Sun Feb 22 11:12:19 EST 2009


I think this gets you what you want:

majority nums = find (\x -> fromIntegral (length x) > (fromIntegral
(length nums)) / 2) (group(sort(nums)))

It returns a "Maybe" list containing all majority elements. Something
that avoids the sort would be faster though.

-Keith

> Message: 8
> Date: Sun, 22 Feb 2009 01:15:30 +0000
> From: "Matthew J. Williams" <matthewjwilliams1 at googlemail.com>
> Subject: [Haskell-beginners] Majority Element
> To: beginners at haskell.org
> Message-ID: <49a0a726.278e420a.6b19.ffffe361 at mx.google.com>
> Content-Type: text/plain; charset="us-ascii"; format=flowed
>
> Dear listers
>
>        What is a majority element in an array?
>
>        Sincerely
>        Matthew J. Williams
>
>
>
> ------------------------------
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list