Created
December 11, 2016 23:47
-
-
Save JessicaGillan/98fa0cb3b45251d3b6129476f2df85ab to your computer and use it in GitHub Desktop.
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 first_nonrepeated_char( string ) | |
chars = string.gsub(" ", "").chars | |
chars.each_with_index do |ch, index| | |
# Skip characters that we have already marked as repeats. | |
next if ch.empty? | |
match = false | |
# Compare each character with the characters that follow it. | |
((index+1)..(chars.length - 1)).each do |index2 | | |
# If we find a match, set the matching character to "" so | |
# we don't have to check it again | |
if ch == chars[index2] | |
match = true | |
chars[ index2 ] = "" | |
end | |
end | |
# Return the character and end unless it had a match | |
return ch unless match | |
end | |
# return nil to indicate that every character had a repeat | |
return nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment