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 JavaScript1. 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); Outputtrue 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 SetA 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); Outputtrue 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() MethodsThe 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 ExpressionsIf 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); Outputtrue (\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); Outputtrue [...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.ConclusionThe 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. Comment More infoAdvertise with us Next Article JavaScript Program to Check for Repeated Characters in a String J janhvisiui7x Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 More Similar Reads JavaScript Program to Find Kâth Non-Repeating Character in String The K'th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K'th unique character encountered, it is returned as the result. This operation helps 6 min read JavaScript Program to Get a Non-Repeating Character From the Given String In JavaScript, we can find the non-repeating character from the input string by identifying the characters that occur only once in the string. There are several approaches in JavaScript to get a non-repeating character from the given string which are as follows: Table of Content Using indexOf and la 3 min read JavaScript Program to Remove Consecutive Duplicate Characters From a String We are going to implement a JavaScript program to remove consecutive duplicate characters from a string. In this program, we will eliminate all the consecutive occurrences of the same character from a string.Example:Input: string: "geeks" Output: "geks"Explanation :consecutive "e" should be removedT 5 min read JavaScript Program to Print All Duplicate Characters in a String In this article, we will learn how to print all duplicate characters in a string in JavaScript. Given a string S, the task is to print all the duplicate characters with their occurrences in the given string. Example: Input: S = âgeeksforgeeksâOutput:e, count = 4g, count = 2k, count = 2s, count = 2Ta 5 min read JavaScript Program to Find the First Repeated Word in String Given a string, our task is to find the 1st repeated word in a string. Examples: Input: âRavi had been saying that he had been thereâOutput: hadInput: âRavi had been saying thatâOutput: No RepetitionBelow are the approaches to Finding the first repeated word in a string: Table of Content Using SetUs 4 min read JavaScript Program to Check if Two Strings are Same or Not In this article, we are going to implement a JavaScript program to check whether two strings are the same or not. If they are the same then we will return true else we will return false.Examples: Input: str1 = Geeks, str2 = GeeksOutput: True. Strings are the SameInput: str1 = Geeks, str2 = GeekOutpu 4 min read JavaScript Program to Find Missing Characters to Make a String Pangram We have given an input string and we need to find all the characters that are missing from the input string. We have to print all the output in the alphabetic order using JavaScript language. Below we have added the examples for better understanding. Examples: Input : welcome to geeksforgeeksOutput 6 min read JavaScript Program to Count the Occurrences of a Specific Character in a String In this article, we will see how to count the frequency of a specific character in a string with JavaScript. Counting the frequency of a specific character in a string is a common task in JavaScript. Example: Input : S = âgeeksforgeeksâ and c = âeâOutput : 4Explanation: âeâ appears four times in str 3 min read JavaScript Program to Check Equal Character Frequencies We have given a String to ensure it has equal character frequencies, if not, equate by adding required characters and printing the final string in the console.Example:Input: test_str = âgeeksforgeeksâ Output: geeksforgeeksggkkssfffooorrr Explanation: Maximum characters are 4 of âeâ. Other character 6 min read JavaScript Program to Find Uncommon Characters of the two Strings In JavaScript, finding uncommon characters in two strings is the process of identifying the characters that exist in one string but not in the other string. We can find these uncommon characters using various approaches that are mentioned below: Table of Content Using SetsUsing Array.filter() method 2 min read Like