0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Check if String Has All Unique Characters

Use a Set to check uniqueness by comparing new Set(string).size === string.length which returns true if all characters are unique, otherwise false.
📋

Examples

Inputabcde
Outputtrue
Inputhello
Outputfalse
Input
Outputtrue
🧠

How to Think About It

To check if all characters in a string are unique, think about how you can remember which characters you have seen. If you find a character again, it means the string is not unique. Using a Set helps because it only stores unique values, so if the size of the Set is the same as the string length, all characters are unique.
📐

Algorithm

1
Get the input string.
2
Create a Set from the string characters.
3
Compare the size of the Set with the length of the string.
4
If they are equal, return true (all unique).
5
Otherwise, return false (duplicates exist).
💻

Code

javascript
function hasAllUniqueChars(str) {
  return new Set(str).size === str.length;
}

console.log(hasAllUniqueChars('abcde')); // true
console.log(hasAllUniqueChars('hello')); // false
console.log(hasAllUniqueChars('')); // true
Output
true false true
🔍

Dry Run

Let's trace the string 'hello' through the code to see how it detects duplicates.

1

Input string

str = 'hello'

2

Create Set

Set = {'h', 'e', 'l', 'o'} (size 4)

3

Compare sizes

Set size (4) !== str length (5)

4

Return result

false (string has duplicates)

IterationCharacterSet ContentsSet SizeString LengthResult
1h{'h'}15Continue
2e{'h','e'}25Continue
3l{'h','e','l'}35Continue
4l{'h','e','l'}35Duplicate found
5o{'h','e','l','o'}45End
💡

Why This Works

Step 1: Using a Set

A Set stores only unique values, so when we add characters from the string, duplicates are ignored.

Step 2: Comparing sizes

If the number of unique characters (Set size) matches the string length, it means no duplicates exist.

Step 3: Return boolean

The comparison returns true if all characters are unique, otherwise false.

🔄

Alternative Approaches

Using a loop and a Set
javascript
function hasAllUniqueChars(str) {
  const seen = new Set();
  for (const char of str) {
    if (seen.has(char)) return false;
    seen.add(char);
  }
  return true;
}

console.log(hasAllUniqueChars('abcde')); // true
console.log(hasAllUniqueChars('hello')); // false
This method stops early when a duplicate is found, which can be faster for long strings with duplicates.
Using an object to track characters
javascript
function hasAllUniqueChars(str) {
  const chars = {};
  for (let i = 0; i < str.length; i++) {
    if (chars[str[i]]) return false;
    chars[str[i]] = true;
  }
  return true;
}

console.log(hasAllUniqueChars('abcde')); // true
console.log(hasAllUniqueChars('hello')); // false
This uses a plain object to track seen characters, which works well but is slightly more verbose.

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

Time Complexity

Creating a Set from the string takes O(n) time because it processes each character once.

Space Complexity

The Set stores up to n unique characters, so space is O(n).

Which Approach is Fastest?

Using a loop with early return on duplicates can be faster in practice for strings with duplicates, but the Set size comparison is simpler and concise.

ApproachTimeSpaceBest For
Set size comparisonO(n)O(n)Simple and concise code
Loop with Set and early returnO(n) best, faster on duplicatesO(n)Performance when duplicates appear early
Object trackingO(n)O(n)Compatibility with older JS versions
💡
Use a Set to quickly check uniqueness by comparing its size to the string length.
⚠️
Forgetting that strings with zero or one character are always unique and returning incorrect results.