Skip to content

Instantly share code, notes, and snippets.

@abrahamsangha
Created June 7, 2013 20:03
Show Gist options
  • Save abrahamsangha/5732012 to your computer and use it in GitHub Desktop.
Save abrahamsangha/5732012 to your computer and use it in GitHub Desktop.
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values. If there's only one most-frequent value, it returns a single-element Array.
def mode(array)
hash = Hash.new(0)
array.each { |elem| hash[elem] += 1 }
hash.keep_if { |k, v| v == hash.values.max }.keys
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment