I read of a job interview question to write some code for the following:
Write an efficient function to find the first nonrepeated character in a st
Since we need the first non-repeating character, it is better to use OrderedDict from collections because dict does not guarantee the order of the keys
from collections import OrderedDict
dict_values = OrderedDict()
string = "abcdcd"
for char in string:
if char in dict_values:
dict_values[char] += 1
else:
dict_values[char] = 1
for key,value in dict_values.items():
if value == 1:
print ("First non repeated character is %s"%key)
break
else:
pass