Languages
[Edit]
EN

JavaScript - count character occurrences

7 points
Created by:
Cassia-Mendoza
415

In this article, we would like to show how using JavaScript, count the number of characters that were used in some text.

Quick solution:

// ONLINE-RUNNER:browser;

const countCharacters = (text) => {
	const counts = {};
	for (const entry of input) {
		const count = counts[entry] ?? 0;
		counts[entry] = count + 1;
	}
	return counts;
};

// Usage example:

const input = 'This is some text...';
const counts = countCharacters(input);

for (const key in counts) {
  	console.log(key + ' ' + counts[key]);
}

JavaScript - String (popular problems)

JavaScript - count character occurrences
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join