Created
April 22, 2013 22:18
-
-
Save JFickel/5439052 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) | |
hash = {} | |
array.each do |number| | |
if hash[number] == nil | |
hash[number] = 1 | |
elsif hash[number] | |
hash[number] += 1 | |
end | |
end | |
@counts = [] | |
hash.each do |pair| | |
@counts << pair.last | |
end | |
@highest_count = @counts.max | |
@equal_frequency_response = [] | |
if @counts.count(@highest_count) > 1 | |
hash.each do |pair| | |
if pair.last == @highest_count | |
@equal_frequency_response << pair.first | |
end | |
end | |
return @equal_frequency_response | |
else | |
hash.each do |pair| | |
if pair.last == @highest_count | |
return [pair.first] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment