DEV Community

Shivam Sahu
Shivam Sahu

Posted on

ss

function printDuplicateCharacters(str) {
    // Step 1: Initialize an empty object to store character counts
    let charCount = {};

    // Step 2: Iterate through the string to count each character
    for (let i = 0; i < str.length; i++) {
        let char = str[i];
        if (charCount[char]) {
            charCount[char]++;
        } else {
            charCount[char] = 1;
        }
    }

    // Step 3: Find and print characters with a count greater than one
    for (let char in charCount) {
        if (charCount[char] > 1) {
            console.log(char + ": " + charCount[char]);
        }
    }
}

// Example usage
let inputString = "programming";
printDuplicateCharacters(inputString);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)