Skip to main content

Remove duplicate characters in a given string keeping only the first occurrences. For example, if the input is tree traversal the output will be tre avs.

function removeDuplicates(string) {
  var output = '',
    hash = {};

  for (var i = 0; i < string.length; i++) {
    if (!hash[string[i]]) {
      output += string[i];
    }
    hash[string[i]] = true;
  }

  return output;
};