Last active
August 29, 2015 14:09
-
-
Save mosdevly/1b7581cb8c63d971d305 to your computer and use it in GitHub Desktop.
Create an object that will check the contents of a string for the most common character. It must return the character and the number of that character.
This file contains 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 common(str) | |
most = nil | |
mcount = nil | |
idx1 = 0 | |
while idx1 < str.length | |
char = str[idx1] | |
count = 0 | |
idx2 = 0 | |
while idx2 < str.length | |
if str[idx2] == char | |
count += 1 | |
end | |
idx2 += 1 | |
end | |
p "There are #{count} of #{str[idx1]}." | |
if (mcount == nil) || (count > most) | |
most = char | |
mcount = count | |
end | |
idx1 += 1 | |
end | |
end | |
p common("electrical") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment