Remove Non-Duplicate Characters from String in JavaScript



Non-duplicate characters in a string are those that appear only once and do not repeat within the string, and the strings are a data type that represents a sequence of characters or text. Sometimes in JavaScript, we need to work with strings and manipulate them based on certain conditions. One of the common tasks is to remove characters that only appear once in a string so that we keep only the characters that appear more than once. In this article, we will understand how to remove non-duplicate characters from a string in JavaScript.

Understanding the Problem

Suppose we have a string saying "teeth_foot". We want to remove all the characters from the given string that appear only once so that we will have a string of characters that appear more than once. For example:

Input string we have:

"teeth_foot"

And the Output we want:

"teetoot"

Removing Non Duplicate Characters from String

To remove non-duplicate characters from a string, we are required to write a JavaScript function that takes in a string and returns a new string with all non-duplicate characters removed from it in the following steps:

  • Firstly, have to define a string (for e.g. teeth_foot).
  • Define a function named removeNonDuplicate that takes a string as an argument.
  • Inside the function, convert the string into an array of characters using the split method. This will allow you to work with each character individually.
  • Use the filter method on the strArray to create a new array (such as duplicate Array) that contains only the characters that appear more than once.
  • For each character, check if its first occurrence index is different from its last occurrence index. If they are different, it means the character is a duplicate.
  • Use the join method to combine the characters in duplicateArray back into a single string.
  • Finally, call the removeNonDuplicate function with the input string str and log the result to the console.

Example 1

This example demonstrates the implementation of a function that will remove non-duplicate characters from a string when called.

const str = 'teeth_foot';
const removeNonDuplicate = str => {
    const strArray = str.split("");
    const duplicateArray = strArray.filter(el => {
        return strArray.indexOf(el) !== strArray.lastIndexOf(el);
    });
    return duplicateArray.join("");
};
console.log(removeNonDuplicate(str));

Output

teetoot

Example 2

In the above example, we have defined the string statically, but in this example, we are taking the input string from the user.

// Function to remove non-duplicate characters from a string
const removeNonDuplicate = str => {
    const strArray = str.split(""); 
    // Split the string into an array of characters
    const duplicateArray = strArray.filter(el => {
        // Filter for duplicates
        return strArray.indexOf(el) !== strArray.lastIndexOf(el);
    });
    return duplicateArray.join("");
    // Join duplicates back into a string
};

// Prompt the user for input
const userInput = prompt("Please enter a string:");
// Get string input from the user

// Call the function with the user's input and log the result
console.log(removeNonDuplicate(userInput));

Conclusion

In this article, we have understood how to remove non-duplicate characters from a string and implemented a practical example for this. Using JavaScript's built-in methods like filter, indexOf, and lastIndexOf makes this task easy and efficient.

Updated on: 2025-03-18T13:00:28+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements