Coding: Replace Unique Character with # and Duplicate Character with * in a String in JavaScript

Monu Kumar Modi
JavaScript in Plain English
3 min readOct 19, 2023

--

We will be discussing multiple solutions.

Solution 1:

Introduction: I will explain a JavaScript function that I have developed called replaceUniqueAndDuplicate. This function takes a string as input and replaces each character in the input string with either * if the character is a duplicate or # if the character is unique.

Function Definition: The function replaceUniqueAndDuplicate takes one argument, str, which is the input string. It initializes two variables, result to store the result string, and charCount to keep track of character counts.

Counting Characters: The code enters Step 1, where it iterates through each character in the input string, str. It uses the charCount object to keep track of how many times each character appears in the string. The line charCount[char] = (charCount[char] || 0) + 1; increments the count for each character.

Replacing Characters: In Step 2, the code iterates through the input string again. It checks the count of each character in charCount. If a character appears more than once, it appends a ‘*’ to the result string to indicate that it’s a duplicate character. Otherwise, if the character count is 1 (unique), it appends a ‘#’ to the result string.

Return Result: Finally, the function returns the result string, which contains the modified characters.

Complete Code:

function replaceUniqueAndDuplicate(str) {
let result = '';
let charCount = {};

for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}

for (let char of str) {
if (charCount[char] > 1) {
result += '*';
} else {
result += '#';
}
}

return result;
}

Example Usage

console.log(replaceUniqueAndDuplicate('success')); // Output: *#**#**
console.log(replaceUniqueAndDuplicate('dog')); // Output: ###

Solution 2: Using lastIndexOf() and indexOf() Method

Function Definition:

  1. The replaceUniqueAndDuplicate function accepts a single argument, str, which represents the input string.
  2. It initializes an empty string, the result, which will store the modified string, highlighting unique and duplicate characters.

Iterating Through the String:

  1. We use a for…of the loop to iterate through each character in the input string, str.

Character Identification:

  1. To determine if a character is unique or a duplicate, we compare the last index of the character in the string (str.lastIndexOf(char)) with the current index of the character in the string (str.indexOf(char)).

Appending Symbols:

  1. If the last index of the character is the same as the current index, it indicates that the character appears only once in the string, so we append a ‘#’ to the result string to mark it as a unique character.
  2. If the last index and the current index are different, this implies the character is found multiple times in the string (a duplicate), and we append a ‘*’ to the result.

Result Return:

  1. Finally, the function returns the result string, which now contains the input string with ‘#’ for unique characters and ‘*’ for duplicate characters.

Complete Code:

function replaceUniqueAndDuplicate(str) {
let result = '';

for (let char of str) {

if (str.lastIndexOf(char) === str.indexOf(char)) {
result += '#'; // Unique character
} else {
result += '*'; // Duplicate character
}
}

return result;
}

Example Usage:

console.log(replaceUniqueAndDuplicate('hello')); 
console.log(replaceUniqueAndDuplicate('programming'));

Conclusion:

  1. The replaceUniqueAndDuplicate function offers a concise solution for identifying unique and duplicate characters in strings. Its ability to mark uniqueness with ‘#’ and duplicates with ‘*’ simplifies text processing tasks, making it a valuable tool for JavaScript developers.

If you’re interested in learning more about coding in Javascript, be sure to follow me for more code snippets and examples. I’ll be sharing valuable information and tips on how to master the language and improve your skills as a developer. So, stay tuned for more updates, and let’s continue learning together.

Thanks for reading.Happy learning 😄

In Plain English

Thank you for being a part of our community! Before you go:

--

--