Created
December 22, 2014 03:22
-
-
Save rickhull/3779263e89c4c84ad9bd to your computer and use it in GitHub Desktop.
given a list of items (with repeats), which one has the highest frequency?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp majority(list) do | |
largest = 0 | |
maj = [] | |
list |> Enum.group_by(&(&1)) |> Enum.each(fn {val,lst} -> | |
case length(list) do | |
cnt when cnt > largest -> | |
largest = cnt | |
maj = [val] | |
cnt when cnt == largest -> | |
maj = [val | maj] | |
end | |
end) | |
Enum.sample(maj) | |
end |
chrismccord
commented
Dec 22, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment