mercredi 30 septembre 2015

first non-reocurring letter of a string in ruby

I have a string "teststring" I want to find first non reoccurring character in ruby.

I tried this code

def first_non_repeat_character(teststring)
unique=[]
repeated=[]
for 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`

Aucun commentaire:

Enregistrer un commentaire