
- 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 integers in order using JavaScript
Suppose, we have a long string that represents a number like this −
const str = '11222233344444445666';
We are required to write a JavaScript function that takes in one such string. Our function is supposed to return an object that should assign a unique "id" property to each unique number in the string and one other property "count" that stores the count of the number of times the number appears in the string.
Therefore, for the above string, the output should look like −
const output = { '1': { id: '1', displayed: 2 }, '2': { id: '2', displayed: 4 }, '3': { id: '3', displayed: 3 }, '4': { id: '4', displayed: 7 }, '5': { id: '5', displayed: 1 }, '6': { id: '6', displayed: 3 } };
Example
The code for this will be −
const str = '11222233344444445666'; const countNumberFrequency = str => { const map = {}; for(let i = 0; i < str.length; i++){ const el = str[i]; if(map.hasOwnProperty(el)){ map[el]['displayed']++; }else{ map[el] = { id: el, displayed: 1 }; }; }; return map; }; console.log(countNumberFrequency(str));
Output
And the output in the console will be −
{ '1': { id: '1', displayed: 2 }, '2': { id: '2', displayed: 4 }, '3': { id: '3', displayed: 3 }, '4': { id: '4', displayed: 7 }, '5': { id: '5', displayed: 1 }, '6': { id: '6', displayed: 3 } }
- Related Articles
- Store count of digits in order using JavaScript
- Returning reverse array of integers using JavaScript
- How to store JavaScript functions in a queue and execute in that order?
- Inverting signs of integers in an array using JavaScript
- Capitalize letter in a string in order and create an array to store - JavaScript
- Deep count of elements of an array using JavaScript
- How to get the product of two integers without using * JavaScript
- How to find the binomial coefficient of two integers using JavaScript?
- How to store elements in numeric order for Listview in Android?
- Behavior of + operator in JavaScript to store large numbers?
- JavaScript Program to Count rotations required to sort given array in non-increasing order
- Find the count of substrings in alphabetic order in C++
- Finding clusters of consecutive negative integers in JavaScript
- Count ways to express ‘n’ as sum of odd integers in C++
- Count of pairs in an array that have consecutive numbers using JavaScript

Advertisements