
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Store count of digits in order using JavaScript
Suppose we have a string with digits like this −
const str = '11222233344444445666';
We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string.
Therefore, for this string, the output should be −
const output = { "1": 2, "2": 4, "3": 3, "4": 7, "5": 1, "6": 3 };
Example
Following is the code −
const str = '11222233344444445666'; const mapString = str => { const map = {}; for(let i = 0; i < str.length; i++){ map[str[i]] = (map[str[i]] || 0) + 1; }; return map; }; console.log(mapString(str));
Output
This will produce the following output in console −
{ '1': 2, '2': 4, '3': 3, '4': 7, '5': 1, '6': 3 }
- Related Articles
- Store count of integers in order using JavaScript
- How to count digits of given number? JavaScript
- How to store JavaScript functions in a queue and execute in that order?
- Capitalize letter in a string in order and create an array to store - JavaScript
- Reversed array of digits from number using JavaScript
- Counting specific digits used in squares of numbers using JavaScript
- Replacing digits to form binary using JavaScript
- Count digits in a factorial in C++
- Count Numbers with Unique Digits in C++
- Python – Average digits count in a List
- Deep count of elements of an array using JavaScript
- Java Program to Count Number of Digits in an Integer
- Swift Program to Count Number of Digits in an Integer
- Haskell Program to Count Number of Digits in an Integer
- Kotlin Program to Count Number of Digits in an Integer

Advertisements