Find frequency of all array elements in JavaScript
This post will discuss how to find the frequency of elements in an array in JavaScript.
There are several ways to find the frequency of elements in an array in JavaScript. Here are some examples of how to implement these functions:
1. Using a loop
One of the most common and simple functions is to use an object to store the counts of each element as properties and values. For example, we can use a for loop or a for-of loop to iterate over the array and increment the count for each element in the object. Here is an example of using this function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// an array of numbers const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; // an empty object to store the counts let counts = {}; // a for-of loop to iterate over the array for (const num of arr) { // increment the count for each element or initialize it to 1 counts [num] = counts [num] ? counts [num] + 1 : 1; } // log the counts object to the console console.log(counts); // { '2': 5, '4': 1, '5': 3, '9': 1 } |
2. Using reduce() function
To find the frequency of elements in an array using the reduce() function, we can use an object as the accumulator, and use the array elements as the keys of the object. For each element, we can increment the value of the corresponding key by one, or initialize it to one if it does not exist. This way, we can create an object that stores the frequency of each element in the array. For example, we can use the reduce() function to accumulate the counts in an object like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// create an array of numbers const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; // use the reduce function to create an object with the counts const counts = arr.reduce((obj, num) => { // if the element already exists in the object, increment its count if (obj[num]) { obj[num]++; } else { // otherwise, initialize its count to 1 obj[num] = 1; } // return the updated object return obj; }, {}); // pass an empty object as the initial value // log the counts object to the console console.log(counts); // { '2': 5, '4': 1, '5': 3, '9': 1 } |
3. Using Lodash or Underscore Library
A third way is to use a library like Lodash or Underscore that provides a function to count the occurrences of array elements. Here are some examples of how to implement these functions:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// import the library const _ = require("lodash"); // or const _ = require("underscore"); // create an array of numbers const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; // use the _.countBy function to create an object with the counts const counts = _.countBy(arr); // log the counts object to the console console.log(counts); // { '2': 5, '4': 1, '5': 3, '9': 1 } |
4. Using filter() function
Another way to find the frequency of elements in an array is to use the filter() function. These functions take a callback function that can perform some operation on each element of the array and return a new array or a single value. For example, we can use the filter function to create a new array with only the elements that match a certain condition and then get its length. Here is an example of using this function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// an array of numbers const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; // a function that takes an array and an element as parameters function count(arr, element) { // return the length of the new array with only the matching elements return arr.filter(x => x === element).length; } console.log(count(arr, 5)); // 3 console.log(count(arr, 2)); // 5 console.log(count(arr, 9)); // 1 console.log(count(arr, 4)); // 1 |
That’s all about finding the frequency of elements in an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)