Remove duplicate characters from string

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

       function removeDuplicates(string){
          return string.split('').filter((item, pos, self)=> self.indexOf(item) == pos).join('');
       }
    

    the filter will remove all characters has seen before using the index of item and position of the current element

提交回复
热议问题