Skip to content

Instantly share code, notes, and snippets.

@tlands
Created June 26, 2013 02:57
Show Gist options
  • Save tlands/5864426 to your computer and use it in GitHub Desktop.
Save tlands/5864426 to your computer and use it in GitHub Desktop.
Finding the mode of an array
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