Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Building frequency map of all the elements in an array JavaScript
Building a frequency map (also called a frequency counter) is a common programming task that helps count how many times each element appears in an array. This technique is useful for data analysis, finding duplicates, or solving algorithmic problems.
A frequency map returns an object where each unique element from the array becomes a key, and its corresponding value represents how many times that element appears.
Basic Approach Using forEach()
The most straightforward method is to iterate through the array and build an object that tracks the count of each element:
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));
{ '1': 1, '2': 3, '3': 2, '4': 2, '5': 4, '6': 2, '7': 2, '8': 3 }
Using Logical OR Operator
We can simplify the conditional logic using the logical OR operator to provide a default value:
const getFrequencySimple = (array) => {
const map = {};
array.forEach(item => {
map[item] = (map[item] || 0) + 1;
});
return map;
};
const numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
console.log(getFrequencySimple(numbers));
{ '1': 1, '2': 2, '3': 3, '4': 4 }
Using reduce() Method
The reduce() method provides a more functional programming approach to building the frequency map:
const getFrequencyReduce = (array) => {
return array.reduce((map, item) => {
map[item] = (map[item] || 0) + 1;
return map;
}, {});
};
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
console.log(getFrequencyReduce(fruits));
{ apple: 3, banana: 2, orange: 1 }
Using Map Object (ES6)
For better performance with large datasets or when keys might not be strings, you can use the ES6 Map object:
const getFrequencyMap = (array) => {
const map = new Map();
array.forEach(item => {
map.set(item, (map.get(item) || 0) + 1);
});
return map;
};
const mixed = [1, '1', true, 1, 'hello', true, 'hello'];
const freqMap = getFrequencyMap(mixed);
// Convert Map to object for display
const result = Object.fromEntries(freqMap);
console.log(result);
{ '1': 2, true: 2, hello: 2 }
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
| forEach with if/else | High | Good | Beginners, clear logic |
| forEach with OR operator | Medium | Good | Concise code |
| reduce() | Medium | Good | Functional programming style |
| Map object | Medium | Best | Large datasets, mixed key types |
Common Use Cases
Frequency maps are commonly used for:
- Finding duplicate elements in arrays
- Counting character frequencies in strings
- Data analysis and statistics
- Solving algorithmic problems like finding majority elements
Conclusion
Building frequency maps is a fundamental technique in JavaScript programming. Choose the forEach approach for simplicity, reduce() for functional style, or Map objects when working with large datasets or mixed key types.
