Created
December 12, 2016 00:11
-
-
Save JessicaGillan/68836c8c8b03cacf117e40b1acbed754 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_eff( string ) | |
# Create a Hash with default values of 0 | |
char_counts = Hash.new( 0 ) | |
# Get rid of spaces | |
chars = string.gsub(" ", "").chars | |
# Increment the count the hash table for each character | |
chars.each do |char| | |
char_counts[char] += 1 | |
end | |
# Find the first character with a count of 1 in the hash table | |
chars.each do |char| | |
return char if char_counts[char] == 1 | |
end | |
# return nil if all characters were repeats | |
nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment