Languages
[Edit]
EN

JavaScript - count letter occurrences

7 points
Created by:
JustMike
28860

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]);
}
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