How can I find the first non-recurring letter of a string in Ruby? -
i have string "teststring"
. want find first non-recurring character in ruby.
i have python code:
def first_non_repeat_character(teststring) unique=[] repeated=[] character in teststring: if character in unique: unique.remove(character) repeated.append(character) else: if not character in repeated: unique.append(character) if len(unique): return unique[0] else: return false
def first_non_repeat_character(string) string.chars.find { |character| string.count(character) == 1 } end first_non_repeat_character('teststring') # => "e"
Comments
Post a Comment