JavaScript – How To Find Unique Characters of a String?
Here are the various methods to find the unique characters of a string in JavaScript.
1. Using Set (Most Common)
The Set object is one of the easiest and most efficient ways to find unique characters. It automatically removes duplicates.
const s1 = "javascript";
const s2 = [...new Set(s1)];
console.log(s2);
Output
[ 'j', 'a', 'v', 's', 'c', 'r', 'i', 'p', 't' ]
- Simple and concise.
- Works efficiently for all string sizes.
- Removes duplicates automatically.
2. Using the Spread Operator with Set
Combining the Set object with the spread operator provides a clean and modern way to extract unique characters.
const s1 = "javascript";
const s2 = Array.from(new Set(s1));
console.log(s2);
Output
[ 'j', 'a', 'v', 's', 'c', 'r', 'i', 'p', 't' ]
Similar to the Set approach but demonstrates the use of Array.from().
3. Using indexOf() and lastIndexOf() Methods
This approach identifies unique characters by checking if the first and last occurrence of each character is the same.
const s1 = "javascript";
const s2 = s1.split("").filter((char, index, arr) =>
arr.indexOf(char) === arr.lastIndexOf(char));
console.log(s2);
Output
[ 'j', 'v', 's', 'c', 'r', 'i', 'p', 't' ]
- A functional approach without external objects or utilities.
- Useful for small strings where performance isn’t critical.
4. Using Object Property Lookup
This method uses an object to count character occurrences and filters unique characters.
const s = "javascript";
const count = {};
const unique = [];
// Count occurrences
for (const char of s) {
count[char] = (count[char] || 0) + 1;
}
// Extract unique characters
for (const char in count) {
if (count[char] === 1) unique.push(char);
}
console.log(unique);
Output
[ 'j', 'v', 's', 'c', 'r', 'i', 'p', 't' ]
- Explicitly counts occurrences.
- Best for strings where you also need to track character frequencies.
5. Using Regular Expressions
Regular expressions can be used to match each character and check its occurrences globally.
const s1 = "javascript";
const s2= s1.split("").filter((char) =>
s1.match(new RegExp(char, "g")).length === 1);
console.log(s2);
Output
[ 'j', 'v', 's', 'c', 'r', 'i', 'p', 't' ]
- Useful when handling strings with dynamic match requirements.
- Compact but less efficient for large strings.
6. Using Reduce() Method
The reduce() method can also be used to count characters and extract unique ones.
const s = "javascript";
const count = s.split("").reduce((acc, char) => {
acc[char] = (acc[char] || 0) + 1;
return acc;
}, {});
const unique = Object.keys(count).filter((char) =>
count[char] === 1);
console.log(unique);
- Great for functional programming.
- Combines counting and filtering in one step.
7. Naive Approach (Manual Loop)
The naive approach involves iterating through the string and manually checking for duplicates.
const s1 = "javascript";
let s2 = "";
for (const char of s1) {
if (!s2.includes(char)) {
s2 += char;
}
}
console.log(s2.split(""));
Output
[ 'j', 'a', 'v', 's', 'c', 'r', 'i', 'p', 't' ]
- Suitable for beginners.
- Does not rely on advanced concepts.
Which Approach to Use?
Approach | When to Use |
---|---|
Using Set | Best for simplicity, readability, and efficiency. |
Using Spread Operator | Ideal when combined with modern JavaScript features. |
Using indexOf() | Useful for small strings; functional and concise. |
Using Object Property Lookup | Best when you also need to count character frequencies. |
Using Regular Expressions | Suitable for complex matching requirements. |
Using Reduce | Great for functional programming styles and chaining. |
Naive Approach | Simple and beginner-friendly for small-scale use cases. |
For most cases, Set or the spread operator with Set is recommended due to its simplicity and efficiency. Other methods can be used for specific requirements like counting frequencies or functional chaining.