Open In App

Replace Duplicate Occurrence in a String in JavaScript

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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:

Using Regular Expression

A 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);


Output

GeksForGeks

Using Set and join

You 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);


Output

GeksFor

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);


Output

GeeksFore

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);


Output

GeksFor


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads