Remove duplicates from a given string using Set

In this approach, we first convert the string str into an array using the split(”) method. Then, we use the Set constructor to create a new set from the array, which removes duplicates.

Below is the implementation of above approach:

JavaScript
function removeDuplicate(str) {
    let charArray = str.split('');
    let uniqueChars = [...new Set(charArray)];
    return uniqueChars.join('');
}
let str = 'w3wiki';
console.log(removeDuplicate(str));

Output
geksfor

Time Complexity: O(n)

Auxiliary Space: O(n)

Please refer complete article on Remove duplicates from a given string for more details!



Javascript Program To Remove Duplicates From A Given String

Write a javascript program for a given string S which may contain lowercase and uppercase characters. The task is to remove all duplicate characters from the string and find the resultant string.

Note: The order of remaining characters in the output should be the same as in the original string.
Example:

Input: Str = w3wiki
Output: geksfor
Explanation: After removing duplicate characters such as e, k, g, s, we have string as “geksfor”.


Input: Str = HappyNewYear
Output: HapyNewYr
Explanation: After removing duplicate characters such as p, e, a, we have string as “HapyNewYr”.

Naive Approach:

Iterate through the string and for each character check if that particular character has occurred before it in the string. If not, add the character to the result, otherwise the character is not added to result.

Below is the implementation of above approach:

Javascript
<script>

// JavaScript program to remove duplicate character
// from character array and print in sorted
// order
function removeDuplicate(str, n)
    {
        // Used as index in the modified string
        var index = 0;

        // Traverse through all characters
        for (var i = 0; i < n; i++)
        {

            // Check if str[i] is present before it 
            var j;
            for (j = 0; j < i; j++) 
            {
                if (str[i] == str[j])
                {
                    break;
                }
            }

            // If not present, then add it to
            // result.
            if (j == i) 
            {
                str[index++] = str[i];
            }
        }
        
        return str.join("").slice(str, index);
    }

    // Driver code
        var str = "w3wiki".split("");
        var n = str.length;
        document.write(removeDuplicate(str, n));
    
// This code is contributed by shivanisinghss2110

</script>

Output:  

geksfor

Time Complexity : O(n * n) 
Auxiliary Space : O(1) , Keeps order of elements the same as input. 

Similar Reads

Remove duplicates from a given string using Hashing

Iterating through the given string and use a map to efficiently track of encountered characters. If a character is encountered for the first time, it’s added to the result string, Otherwise, it’s skipped. This ensures the output string contains only unique characters in the same order as the input string....

Remove duplicates from a given string using Set

In this approach, we first convert the string str into an array using the split(”) method. Then, we use the Set constructor to create a new set from the array, which removes duplicates....