- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Frequency distribution of elements - JavaScript
We are required to write a JavaScript function that contains the following string −
const str = 'This string will be used to calculate frequency distribution';
We need to return an object that represents the frequency distribution of various elements present in the array.
Example
Following is the code −
const str = 'This string will be used to calculate frequency distribution'; const frequencyDistribution = str => { const map = {}; for(let i = 0; i < str.length; i++){ map[str[i]] = (map[str[i]] || 0) + 1; }; return map; }; console.log(frequencyDistribution(str));
Output
Following is the output in the console −
{ T: 1, h: 1, i: 6, s: 4, ' ': 8, t: 5, r: 3, n: 3, g: 1, w: 1, l: 4, b: 2, e: 5, u: 4, d: 2, o: 2, c: 3, a: 2, f: 1, q: 1, y: 1 }
- Related Articles
- Frequency Distribution
- Explain the difference between a frequency distribution and a cumulative frequency distribution.
- Frequency of smaller and larger elements - JavaScript
- What is frequency distribution ?
- Define cumulative frequency distribution.
- Sorting array according to increasing frequency of elements in JavaScript
- Function for counting frequency of space separated elements in JavaScript
- Sorting array based on increasing frequency of elements in JavaScript
- Building frequency map of all the elements in an array JavaScript
- Frequency of elements of one array that appear in another array using JavaScript
- Find the mean, mode and median of the following frequency distribution:
- Looping through and getting frequency of all the elements in an array JavaScript
- List frequency of elements in Python
- Python – Fractional Frequency of elements in List
- Find the missing frequencies in the following frequency distribution if it is known that the mean of the distribution is 50.

Advertisements