Created
June 26, 2013 02:57
-
-
Save tlands/5864426 to your computer and use it in GitHub Desktop.
Finding the mode of an array
This file contains 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(array) | |
count = Hash.new(0) # specify default object to 0, because you can't += 1 to nil. | |
array.each { |elem| count[elem] += 1 } | |
count.keep_if { | key, val | val == count.values.max }.keys | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment