JavaScript Program to Print All Duplicate Characters in a String
In this article, we will learn how to print all duplicate characters in a string in JavaScript. Given a string S, the task is to print all the duplicate characters with their occurrences in the given string.
Example:
Input: S = “w3wiki”
Output:
e, count = 4
g, count = 2
k, count = 2
s, count = 2
Table of Content
- Using For Loop in JavaScript
- Using Sorting in JavaScript
- Using Hashing in JavaScript
- Using Set()
- Using Reduce Method in JavaScript
Examples of Print All Duplicate Characters in a String In JavaScript
1. Using For Loop in JavaScript
In this approach, an object named charCount
to effectively maintain the frequency of characters in the string. It sequentially processes each character in the input string, increasing its count in the charCount
object. Once the character frequencies are established, the code then traverses through the charCount
object. It selectively prints characters that have occurrences greater than one, thereby accurately showcasing the duplicate characters along with the corresponding count of duplications.
Example: In this example, we are using a for loop.
function printDups(str) {
let charCount = {};
for (let i = 0; i < str.length; i++) {
let character = str[i];
charCount[character] =
(charCount[character] || 0) + 1;
}
for (let char in charCount) {
if (charCount[char] > 1) {
console.log(
char +
", count = " +
charCount[char]
);
}
}
}
let str = "w3wiki";
printDups(str);
Output
g, count = 2 e, count = 4 k, count = 2 s, count = 2
2. Using Sorting in JavaScript
- Sort the given string.
- Loop through the sorted string to find the duplicates.
- If the next character is the same as the current character then we keep on counting the occurrence of that char.
- If the count is greater than one then we print the character and its count.
Example: In this example we are using Sorting
function printDuplicates(str) {
let len = str.length;
// Sorting the string
str = str.split('').sort().join('');
// Loop through the sorted string to find duplicates
for (let i = 0; i < len; i++) {
let count = 1;
// Counting the occurrences of each character
while (i < len - 1 && str[i] == str[i + 1]) {
count++;
i++;
}
// Printing the duplicate character and its count
if (count > 1) {
console.log(str[i] + ", count = " + count);
}
}
}
let str = "w3wiki";
printDuplicates(str);
Output
e, count = 4 g, count = 2 k, count = 2 s, count = 2
3. Using Hashing in JavaScript
- Declare a unordered map of the char-int pair.
- Traverse the string using a loop and increase the count of the present char in the map.
- Iterate through the map and print a character that has a value greater than one in map.
Example: In this example we are using Hashing.
// JavaScript program to count all duplicates
// from string using maps
function printDups(str) {
let count = new Map();
for (let i = 0; i < str.length; i++) {
if (count.has(str[i])) {
count.set(
str[i],
count.get(str[i]) + 1
);
} else {
count.set(str[i], 1);
}
//increase the count of characters by 1
}
//iterating through the unordered map
for (let [it, it2] of count) {
if (it2 > 1)
//if the count of characters is
//greater than 1 then duplicate found
console.log(it, ", count = ", it2);
}
}
/* Driver code*/
let str = "w3wiki";
printDups(str);
Output
g , count = 2 e , count = 4 k , count = 2 s , count = 2
4. Using Set()
This approach uses two Sets to track characters in a string, seen set to store characters encountered, and duplicates set to store duplicate characters. It iterates over the string, adding characters to seen if they are not already there, or to duplicates if they are.
Example: In this example we are using Set().
function findDuplicates(str) {
const seen = new Set();
const duplicates = new Set();
for (let char of str) {
if (seen.has(char)) {
duplicates.add(char);
} else {
seen.add(char);
}
}
duplicates.forEach(char => {
const count = str.split(char).length - 1;
console.log(`${char}, count= ${count}`);
});
}
const str = "w3wiki";
findDuplicates(str);
Output
e, count= 4 g, count= 2 k, count= 2 s, count= 2
5. Using Reduce Method in JavaScript
In this approach, we will use the reduce method to create an object that tracks the count of each character. Then, we will loop through this object to identify and print characters that have a count greater than one.
Example: Using the reduce Method
function printDupsUsingReduce(str) {
// Using reduce to build the character count object
let charCount = str.split('').reduce((acc, char) => {
acc[char] = (acc[char] || 0) + 1;
return acc;
}, {});
// Iterating over the charCount object to print duplicates
Object.keys(charCount).forEach(char => {
if (charCount[char] > 1) {
console.log(`${char}, count = ${charCount[char]}`);
}
});
}
let str = "w3wiki";
printDupsUsingReduce(str);
Output
g, count = 2 e, count = 4 k, count = 2 s, count = 2