Created
November 8, 2013 02:14
-
-
Save cprater/7365212 to your computer and use it in GitHub Desktop.
Calculating 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
# 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