Skip to content

Instantly share code, notes, and snippets.

@michaeldv
Forked from auser/snippet.rb
Created February 17, 2010 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeldv/306409 to your computer and use it in GitHub Desktop.
Save michaeldv/306409 to your computer and use it in GitHub Desktop.
def mode(arr=[])
hsh = arr.inject(Hash.new(1)) { |hash, item| hash[item] += 1; hash }
max = hsh.values.max
hsh.inject([]) { |arr, item| arr << item[0] if item[1] >= max; arr}
end
p mode %w(a b a a b c dd e e e) # ["e", "a"]
p mode %w(a b a a b c dd e e) # ["a"]
p mode %w(snowboarding is more than fun. super fun.) # ["fun."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment