Created
May 1, 2015 18:18
-
-
Save chellberg/730a4046b94c40d8e453 to your computer and use it in GitHub Desktop.
indyrb interview problem
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 counted_recurring_elements_in(array) | |
counts_by_number = array.inject({}) do |memo, element| | |
memo[element] = memo[element].to_i + 1 # I like this trick but I wish it didn't rely on knowing nil.to_i == 1 | |
memo | |
end | |
recurring_counts_by_number = counts_by_number.select do |number, count| | |
count > 1 | |
end | |
recurring_counts_by_number | |
end |
I believe this does the same thing while removing the hack you don't like.
counts_by_number = array.inject(Hash.new(0)) do |memo, element|
memo[element] += 1
memo
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nil.to_i == 0
*