Remove duplicate characters from string

前端 未结 28 835
猫巷女王i
猫巷女王i 2020-12-01 13:09

I have to make a function in JavaScript that removes all duplicated letters in a string. So far I\'ve been able to do this: If I have the word \"anaconda\" it shows me as a

28条回答
  •  -上瘾入骨i
    2020-12-01 13:32

    Here's what I used - haven't tested it for spaces or special characters, but should work fine for pure strings:

    function uniquereduce(instring){ 
        outstring = ''
        instringarray = instring.split('')
        used = {}
        for (var i = 0; i < instringarray.length; i++) {
            if(!used[instringarray[i]]){
                used[instringarray[i]] = true
                outstring += instringarray[i]
            }
        }
        return outstring
    }
    

提交回复
热议问题