Skip to content

Instantly share code, notes, and snippets.

@cust0m
Created April 11, 2017 23:25
Show Gist options
  • Save cust0m/1456790698171045090959af602e9cea to your computer and use it in GitHub Desktop.
Save cust0m/1456790698171045090959af602e9cea to your computer and use it in GitHub Desktop.
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