Created
April 11, 2017 23:25
-
-
Save cust0m/1456790698171045090959af602e9cea 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 MostPupularNumber array, size | |
return array[0] if size == 1 | |
most_popular = nil | |
hash = {} | |
array.each do |item| | |
item_key = item.to_s | |
if hash.has_key?(item_key) | |
hash[item_key] += 1 | |
else | |
hash[item_key] = 1 | |
end | |
end | |
hash.each do |k,v| | |
if most_popular == nil | |
most_popular = k | |
elsif hash[most_popular] < v | |
most_popular = k | |
elsif hash[most_popular] == v && most_popular.to_i > k.to_i | |
most_popular = k | |
end | |
end | |
return most_popular | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment