Last active
August 29, 2015 14:08
-
-
Save bionicpill/63ba21c8b9a4c85879ea to your computer and use it in GitHub Desktop.
Find most popular item in the array
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
#! /usr/bin/ruby | |
def popular(a) | |
b = a.uniq | |
c = 0 | |
b.map {|x| c = x if a.count(x) > c} | |
c | |
end | |
def test | |
failed = false | |
if popular([1, 1, 1, 2, 2, 5, 5, 5, 5, 4, 4]) != 5 | |
failed = true | |
puts "fail test 1" | |
end | |
if popular([1, 1, 1, 2, 2, 5, 5, 5, 1, 1, 1, 1, 1]) != 1 | |
failed = true | |
puts "fail test 2" | |
end | |
if popular([9, 9, 1, 1, 2, 2, 2]) != 2 | |
failed = true | |
puts "fail test 3" | |
end | |
puts "Success" unless failed | |
end | |
test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment