Skip to content

Instantly share code, notes, and snippets.

@JessicaGillan
Created December 11, 2016 23:31
Show Gist options
  • Save JessicaGillan/18c37f6676544783da0b038db3015d42 to your computer and use it in GitHub Desktop.
Save JessicaGillan/18c37f6676544783da0b038db3015d42 to your computer and use it in GitHub Desktop.
# 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