Skip to content

Instantly share code, notes, and snippets.

@JessicaGillan
Created December 12, 2016 00:11
Show Gist options
  • Save JessicaGillan/68836c8c8b03cacf117e40b1acbed754 to your computer and use it in GitHub Desktop.
Save JessicaGillan/68836c8c8b03cacf117e40b1acbed754 to your computer and use it in GitHub Desktop.
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