-
-
Save michaeldv/306409 to your computer and use it in GitHub Desktop.
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
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