Skip to content

Instantly share code, notes, and snippets.

@mosdevly
Last active August 29, 2015 14:09
Show Gist options
  • Save mosdevly/1b7581cb8c63d971d305 to your computer and use it in GitHub Desktop.
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.
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