Remove duplicate characters from string

前端 未结 28 900
猫巷女王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:50

    This code worked for me on removing duplicate(repeated) characters from a string (even if its words separated by space)

    Link: Working Sample JSFiddle

    /* This assumes you have trim the string and checked if it empty */
    function RemoveDuplicateChars(str) {
       var curr_index = 0;
       var curr_char;
       var strSplit;
       var found_first;
       while (curr_char != '') {
          curr_char = str.charAt(curr_index);
          /* Ignore spaces */
          if (curr_char == ' ') {
             curr_index++;
             continue;
          }
          strSplit = str.split('');
          found_first = false;
          for (var i=0;i str.length-1) return str;
        return str.substr(0,index) + chr + str.substr(index+1);
    }
    

提交回复
热议问题