Remove duplicate characters from string

前端 未结 28 944
猫巷女王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条回答
  •  感动是毒
    2020-12-01 13:51

    DEMO

    function find_unique_characters( string ){
        unique=[];
        while(string.length>0){
            var char = string.charAt(0);
            var re = new RegExp(char,"g");
            if (string.match(re).length===1) unique.push(char);
            string=string.replace(re,"");
        }        
        return unique.join("");
    }
    console.log(find_unique_characters('baraban')); // rn
    console.log(find_unique_characters('anaconda')); //cod
    ​
    

提交回复
热议问题