Skip to content

Instantly share code, notes, and snippets.

@cprater
Created November 8, 2013 02:14
Show Gist options
  • Save cprater/7365212 to your computer and use it in GitHub Desktop.
Save cprater/7365212 to your computer and use it in GitHub Desktop.
Calculating the mode of an array
# Create a hash to put the Key: integer, value: times it appears in
# Create a loop ti fill the Hash with values
# sort the hash by the highest value
# Return the key of the highest value(s)
#
#
def mode(array)
num = Hash.new(0)
array.each do |i|
num[i] += 1
end
max = num.values.sort
return num.key(max.last)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment