Remove duplicate characters from string

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

    Just wanted to add my solution for fun:

    function removeDoubles(string) {
      var mapping = {};
      var newString = '';
    
      for (var i = 0; i < string.length; i++) {
        if (!(string[i] in mapping)) {
          newString += string[i];
          mapping[string[i]] = true;
        }
      }
      return newString;
    }
    

提交回复
热议问题