Duplicate Character Counter in JavaScript

Published on 06 May 2026 (Updated: 06 May 2026)

Welcome to the Duplicate Character Counter in JavaScript page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

"use strict";

const USAGE = "Usage: please provide a string";

const isAlphaNum = (c) => /^[a-z0-9]$/i.test(c);

const countDuplicates = (str) => {
  const counts = new Map();

  for (const ch of str) {
    if (isAlphaNum(ch)) {
      counts.set(ch, (counts.get(ch) ?? 0) + 1);
    }
  }

  const duplicates = [];

  for (const [ch, count] of counts) {
    if (count > 1) {
      duplicates.push(`${ch}: ${count}`);
    }
  }

  return duplicates.length > 0
    ? duplicates.join("\n")
    : "No duplicate characters";
};

const run = () => {
  const [, , input] = process.argv;

  if (!input) {
    console.error(USAGE);
    process.exit(1);
  }

  console.log(countDuplicates(input));
};

run();

Duplicate Character Counter in JavaScript was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.