
- 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
Building frequency map of all the elements in an array JavaScript
We will be given an array of numbers / strings that contains some duplicate entries, all we have to do is to return the frequency of each element in the array.
Returning an object with an element as key and its value as frequency would be perfect for this situation.
We will iterate over the array with a forEach() loop and keep increasing the count of elements in the object if it already exists otherwise we will create a new property for that element in the object.
And lastly, we will return the object.
The full code for this problem will be −
const arr = [2,5,7,8,5,3,5,7,8,5,3,4,2,4,2,1,6,8,6]; const getFrequency = (array) => { const map = {}; array.forEach(item => { if(map[item]){ map[item]++; }else{ map[item] = 1; } }); return map; }; console.log(getFrequency(arr));
The output in the console will be −
{ '1': 1, '2': 3, '3': 2, '4': 2, '5': 4, '6': 2, '7': 2, '8': 3 }
- Related Articles
- Building a frequency object from an array JavaScript
- Looping through and getting frequency of all the elements in an array JavaScript
- Return a map representing the frequency of each data type in an array in JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Sum of all the non-repeating elements of an array JavaScript
- How to remove all the elements from a map in JavaScript?
- Building an array from a string in JavaScript
- JavaScript Checking if all the elements are same in an array
- How to filter an array from all elements of another array – JavaScript?
- Sorting array according to increasing frequency of elements in JavaScript
- Sorting array based on increasing frequency of elements in JavaScript
- Frequency of elements of one array that appear in another array using JavaScript
- Remove duplicates and map an array in JavaScript
- Building a Map from 2 arrays of values and keys in JavaScript
- Frequency distribution of elements - JavaScript

Advertisements