Last active
June 20, 2018 20:52
-
-
Save parksilk/4448602 to your computer and use it in GitHub Desktop.
Exercise: Calculating the array mode: 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. For example, mode([1,2,3,3]) # => [3]
mode([4.5, 0, 0]) # => [0]
mode([1.5, -1, 1, 1.5]) # => [1.5]
mode([1,1,2,2]) …
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) | |
counter = Hash.new(0) | |
# this creates a new, empty hash with no keys, but makes all defalt values zero. it will be used to store | |
# the information from the array, such that the keys will be each unique number from the array (IOW, if there | |
# are two or more 4's in the array, there will just be one key that is a 4), and the value for each key will | |
# be the number of times that integer appears in the array. | |
array.each do |i| | |
counter[i] += 1 | |
end | |
# this interates throught the array, and for each element it creates a key for that integer (if it hasn't been | |
# created already) and basically pushes a +1 to the value of that key. remember, the values all start at zero | |
# because the "Hash.new" language included "(0)" at the end. | |
mode_array = [] | |
# this creates an empty array, to be filled with the most repeated number(s) from the array. | |
# this will be the array that is returned at the end of the method. | |
counter.each do |k, v| | |
if v == counter.values.max | |
mode_array << k | |
end | |
end | |
# this loop iterates through the counter array. for each key/value pair, it compares the value to | |
# the high value in the array, and if the value it's comparing is equal to the high value, it | |
# adds to the mode array the key associated with that value. | |
mode_array.sort | |
# this returns the mode array (sorted) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You would get better runtime complexity if you moved
counter.values.max
outside of yourcounter.each
loop by assigning it to a variable.