function remove duplicates characters javascript string and array code example

Example 1: javascript remove duplicate letters in a string

function removeDuplicateCharacters(string) {
  return string
    .split('')
    .filter(function(item, pos, self) {
      return self.indexOf(item) == pos;
    })
    .join('');
}
console.log(removeDuplicateCharacters('baraban'));

Example 2: remove repeated characters from a string in javascript

const unrepeated = (str) => [...new Set(str)].join('');

unrepeated("hello"); //➞ "helo"
unrepeated("aaaaabbbbbb"); //➞ "ab"
unrepeated("11112222223333!!!??"); //➞ "123!?"