Skip to content

Instantly share code, notes, and snippets.

@JFickel
Created April 22, 2013 22:18
Show Gist options
  • Save JFickel/5439052 to your computer and use it in GitHub Desktop.
Save JFickel/5439052 to your computer and use it in GitHub Desktop.
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