Open In App

JavaScript Program to Check for Repeated Characters in a String

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are the different methods to check for repeated characters in a string using JavaScript

1. Using a Frequency Counter (Object)

A frequency counter is one of the most efficient ways to check for repeated characters in a string. This approach involves iterating over the string and counting how often each character appears using an object.

JavaScript
let s = 'hello';
let count = {};

for (let i = 0; i < s.length; i++) {
    let char = s[i];
    count[char] = (count[char] || 0) + 1;
}

let repeat = Object.values(count).some(count => count > 1);
console.log(repeat);

Output
true
  • We iterate over the string and build an object charCount to store the frequency of each character.
  • The some() method checks if any character appears more than once.
  • This approach is time-efficient and works well for strings of moderate length.

2. Using a Set

A Set in JavaScript only stores unique values. By iterating over the string and adding characters to the set, we can easily check if a character has already been encountered.

JavaScript
let s = 'hello';
let set = new Set();
let repeat = false;

for (let i = 0; i < s.length; i++) {
    if (set.has(s[i])) {
        repeat = true;
        break;
    }
    set.add(s[i]);
}

console.log(repeat); 

Output
true
  • charSet.add(str[i]) adds each character to the set.
  • charSet.has(str[i]) checks if the character already exists in the set.
  • If a character is found in the set, it means there’s a repeat, and we set hasRepeats to true.

3. Using JavaScript indexOf() and lastIndexOf() Methods

The indexOf() and lastIndexOf() methods can be used to find the first and last occurrence of a character. If the positions are different, the character repeats.

JavaScript
let s = 'hello';
let repeat = false;

for (let i = 0; i < s.length; i++) {
    if (s.indexOf(s[i]) !== s.lastIndexOf(s[i])) {
        repeat = true;
        break;
    }
}

console.log(repeat); 
  • indexOf(str[i]) returns the first occurrence of the character.
  • lastIndexOf(str[i]) returns the last occurrence of the character.
  • If these indices differ, it means the character repeats.

4. Using Regular Expressions

If you're comfortable with regular expressions, you can use them to find repeated characters in a string. A regular expression can be written to match any character that appears more than once.

JavaScript
let s = 'hello';
let repeat = /(\w).*\1/.test(s);

console.log(repeat); 

Output
true
  • (\w) captures a word character (letter, digit, or underscore).
  • .*\1 checks if the same character appears again somewhere later in the string.
  • The test() method returns true if the pattern is matched.

5. Using Array.filter() and indexOf()

If you're familiar with array methods, you can convert the string to an array and use filter() in combination with indexOf() to find repeated characters.

JavaScript
let s = 'hello';
let repeat = [...s].filter((char, index, arr) => 
    arr.indexOf(char) !== index).length > 0;

console.log(repeat); 

Output
true
  • [...str] converts the string into an array of characters.
  • arr.indexOf(char) !== index checks if the current character's index is different from its first occurrence.
  • filter() returns an array of repeated characters, and if its length is greater than 0, it means there are repeated characters.

Conclusion

The most common and efficient way to check for repeated characters in a string is by using a frequency counter (an object to track the count of each character). This approach is efficient, easy to understand, and works well with larger strings. If you prefer a simpler approach, using a Set or indexOf() with lastIndexOf() may be the right choice, especially for shorter strings or when you only care about the first repeat.


Next Article

Similar Reads