Created
December 11, 2016 23:31
-
-
Save JessicaGillan/18c37f6676544783da0b038db3015d42 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
# Define a method that takes a string | |
def first_nonrepeated_char( string ) | |
# Convert that string in to an Array of characters | |
chars = string.gsub(" ", "").chars | |
# Loop over the character Array, and compare | |
# each element to every other element | |
chars.each_with_index do |ch, index| | |
match = false | |
chars.each_with_index do |ch2, index2| | |
match = true if ch == ch2 && index != index2 | |
end | |
# Return the first character without a match | |
return ch unless match | |
end | |
# Return nil if you did not find a non-repeated character | |
return nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment