This chapter introduces how to delete repeated characters in a string. Regardless of whether it has actual value, it is quite good to regard it as a kind of learning about algorithms.
The code is as follows:
function dropRepeat(str){ var result=[]; var hash={}; for(var i=0, elem; i<str.length;i++){ elem=str[i]; if(!hash[elem]){ hash[elem]=true; result=result+elem; } } return result; }
The function in the above code can delete repeated characters in the string, usage example:
dropRepeat("abcdd")
The return value is: abcd.
Let me share with you Python: remove repeated characters in a string
python 2.7: #-*- encoding:utf-8 -*- string = 'abc123456ab2s' r = ''.join(x for i, x in enumerate(string) if string.index(x) == i) print string print r
The output is as follows:
abc123456ab2s
abc123456s