Replace Duplicate Occurrence in a String in JavaScript Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript String is a sequence of characters, typically used to represent text. It is useful to clean up strings by removing any duplicate characters. This can help normalize data and reduce unnecessary bloat. There are several ways to replace duplicate occurrences in a given string using different JavaScript methods which are as follows: Table of Content Using Regular ExpressionUsing Set and joinUsing indexOf() and lastIndexOf()Using filter() and includes()Using Regular ExpressionA regular expression (regex or regexp) is a sequence of characters that is used to define a search pattern. Regular expressions are used in JavaScript to perform pattern matching and "search-and-replace" functions on text. Example: Replacing duplicate occurrences in a string using regex in JavaScript. JavaScript function replaceDuplicates(str) { return str.replace(/(.)\1+/g, "$1"); } const str = "GeeksForGeeks"; const AfterDuplicates = replaceDuplicates(str); console.log(AfterDuplicates); OutputGeksForGeks Using Set and joinYou can store unique values of any kind, including object references and primitive values, using the Set object. A set's data structure (typically an implementation of a hash table) allows you to add, remove, and check for values very quickly. An array's items are joined together into a string using the join() method. The delimiter that is used to divide the array components in the string can be specified with an optional argument. It utilises a comma by default. Example: Replacing duplicates occurrences from a string using Set and join method in JavaScript. JavaScript function replaceDuplicates(str) { const charSet = new Set(str.split("")); return Array.from(charSet).join(""); } const str = "GeeksForGeeks"; const AfterDuplicates = replaceDuplicates(str); console.log(AfterDuplicates); OutputGeksFor Using indexOf() and lastIndexOf()JavaScript's indexOf() and lastIndexOf() functions are used to decide a value's index or position in an array. The index or position of a value's to begin with occurrence in an array is returned by the IndexOf() function. The index or position of a value's last occurrence in an array is returned by the function lastIndexOf(). Example: Replacing duplicates occurrences in a string using indexOf() and lastIndexOf() method in JavaScript. JavaScript function replaceDuplicates(str) { return str .split("") .filter((char, index) => { return str.indexOf(char) === index || str.lastIndexOf(char) !== index; }) .join(""); } const str = "GeeksForGeeks"; const AfterDuplicates = replaceDuplicates(str); console.log(AfterDuplicates); OutputGeeksFore Using filter() and includes()The method filter() creates an additional array including all elements that pass the test performed by the provided function. The includes() method checks if a given value appears in any entry in an array and returns true or false depending on the condition. Example: Replacing duplicates occurrences in the string using filter() and includes() method in JavaScript. JavaScript function replaceDuplicates(str) { return str .split("") .filter((char, index) => { return !str.slice(0, index).includes(char); }) .join(""); } const str = "GeeksForGeeks"; const AfterDuplicates = replaceDuplicates(str); console.log(AfterDuplicates); OutputGeksFor Comment More infoAdvertise with us Next Article Replace Duplicate Occurrence in a String in JavaScript P pranay0911 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Replace All Occurrences of a String in JavaScript? Here are different approaches to replace all occurrences of a string in JavaScript.1. Using string.replace() MethodThe string.replace() method is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.Syntaxstr.replace(replac 2 min read Replace all Occurrences of a Substring in a String in JavaScript Given a String, the task is to replace all occurrences of a substring using JavaScript. The basic method to replace all substring of a string is by using string.replaceAll() method.The string.replaceAll() method is used to search and replace all the matching substrings with a new string. We can use 2 min read JavaScript - How To Count String Occurrence in String? Here are the various methods to count string occurrence in a string using JavaScript.1. Using match() Method (Common Approach)The match() method is a simple and effective way to count occurrences using a regular expression. It returns an array of all matches, and the length of the array gives the co 3 min read How to get nth occurrence of a string in JavaScript ? In this article, the task is to get the nth occurrence of a substring in a string with the help of JavaScript. We have many methods to do this some of which are described below:Approaches to get the nth occurrence of a string:Table of Content Using split() and join() methodsUsing indexOf() methodUsi 5 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read Find Word Character in a String with JavaScript RegExp Here are the different ways to find word characters in a string with JavaScript RegExp1. Using \w to Find Word CharactersThe \w metacharacter in regular expressions matches any word character, which includes:A to Z (uppercase letters)a to z (lowercase letters)0 to 9 (digits)Underscore (_)You can use 3 min read What is jQuery's Equivalent of str_replace in JavaScript? In JavaScript, the equivalent of PHPâs str_replace is the replace method of strings. While jQuery doesn't offer a direct str_replace equivalent, JavaScriptâs replace and replaceAll methods can be used effectively.1. Replace a Single Instance with replaceThis method replaces the first occurrence of a 1 min read How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read How to replace a character at a particular index in JavaScript ? To replace a character from a string there are popular methods available, the two most popular methods we are going to describe in this article. The first method is by using the substr() method. And in the second method, we will convert the string to an array and replace the character at the index. 4 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read Like