0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Remove Duplicates from String

You can remove duplicates from a string in JavaScript by using [...new Set(string)].join(''), which creates a set of unique characters and joins them back into a string.
📋

Examples

Inputhello
Outputhelo
Inputjavascript
Outputjavscript
Inputaaaaaa
Outputa
🧠

How to Think About It

To remove duplicates, think of the string as a list of characters. You want to keep only the first occurrence of each character and ignore the rest. Using a set helps because it automatically stores unique items, so converting the string to a set removes duplicates. Then, you turn the set back into a string.
📐

Algorithm

1
Get the input string.
2
Convert the string into a set to keep only unique characters.
3
Convert the set back into an array.
4
Join the array elements into a single string.
5
Return the new string without duplicates.
💻

Code

javascript
function removeDuplicates(str) {
  return [...new Set(str)].join('');
}

console.log(removeDuplicates('hello'));
console.log(removeDuplicates('javascript'));
console.log(removeDuplicates('aaaaaa'));
Output
helo javscript a
🔍

Dry Run

Let's trace 'hello' through the code

1

Input string

'hello'

2

Convert to Set

Set {'h', 'e', 'l', 'o'}

3

Spread Set into array

['h', 'e', 'l', 'o']

4

Join array into string

'helo'

IterationCharacterSet Content
1h{'h'}
2e{'h', 'e'}
3l{'h', 'e', 'l'}
4l{'h', 'e', 'l'}
5o{'h', 'e', 'l', 'o'}
💡

Why This Works

Step 1: Using Set to Remove Duplicates

A Set in JavaScript stores only unique values, so converting the string to a set removes repeated characters automatically.

Step 2: Spreading Set into Array

The spread operator ... converts the set back into an array so we can join the characters.

Step 3: Joining Characters

Using .join('') combines the array of unique characters back into a single string without duplicates.

🔄

Alternative Approaches

Using filter and indexOf
javascript
function removeDuplicates(str) {
  return str.split('').filter((char, index, arr) => arr.indexOf(char) === index).join('');
}

console.log(removeDuplicates('hello'));
This method checks each character's first occurrence and keeps only unique ones; it is easy to understand but less efficient for long strings.
Using a loop and an object
javascript
function removeDuplicates(str) {
  let seen = {};
  let result = '';
  for (let char of str) {
    if (!seen[char]) {
      seen[char] = true;
      result += char;
    }
  }
  return result;
}

console.log(removeDuplicates('hello'));
This approach manually tracks seen characters and builds the result; it is clear and works well but requires more code.

Complexity: O(n) time, O(n) space

Time Complexity

The code loops through each character once to create the set, so it runs in linear time relative to the string length.

Space Complexity

A set stores unique characters, so extra space grows with the number of unique characters, up to the string length.

Which Approach is Fastest?

Using Set is generally fastest and simplest; filter/indexOf is slower due to repeated searches; manual loop is clear but more verbose.

ApproachTimeSpaceBest For
Set and spreadO(n)O(n)Fastest and simplest for most cases
filter and indexOfO(n²)O(n)Easy to understand but slower for long strings
Loop with objectO(n)O(n)Clear manual control, good for learning
💡
Use new Set() to quickly remove duplicates from any iterable like strings or arrays.
⚠️
Beginners often try to remove duplicates by replacing characters without preserving order or by using loops without tracking seen characters.