Open In App

Count Frequency of an Array Item in JavaScript

Last Updated : 28 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be counting the frequency of an array item in Javascript.

When working with arrays there are situations when we need to count the frequency of an array item. Javascript provides us with some methods to count the occurrences of an item in an array, some of them are explained below:

Approach 1: Using for loop

The Javascript for loop is used to iterate over the elements of an array and is used to count the number of occurrences of elements in the array.

Syntax:

for (statement 1 ; statement 2 ; statement 3){
    // code here...
}

Example: In this example, the for loop iterates over the array of elements and prints the occurrences of all the elements in the console.

Javascript




let arr = [1, 2, 3, 2, 3, 4, 5,
           5, 6, 1, 1, 4, 5, 7, 8, 8];
     
const count = {};
 
for (let i = 0; i < arr.length; i++) {
    let ele = arr[i];
    if (count[ele]) {
        count[ele] += 1;
    } else {
        count[ele] = 1;
    }
}
console.log(count);


Time Complexity: O(n).

Space Complexity: O(n) as a count array has been created.

Output:

{ '1': 3, '2': 2, '3': 2, '4': 2, 
'5': 3, '6': 1, '7': 1, '8': 2 }

Approach 2: Using filter() method

The JavaScript Array filter() Method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method. 

Syntax:

array.filter(callback(element, index, arr), thisValue)

Example: In this example, the filter() method is used to count the frequency of the elements of the array. We are printing the frequency of “1” in the array in the console.

Javascript




let arr = [1, 2, 3, 2, 3, 4, 5,
           5, 6, 1, 1, 4, 5, 7, 8, 8];
 
function count(arr, element) {
    return arr.filter(
        (ele) => ele == element).length;
};
 
console.log(count(arr, 1))


Output:

3

Approach 3: Using reduce() method:

The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. 

Syntax:

array.reduce( function(total, currentValue, 
    currentIndex, arr), initialValue )

Example: In this example, the reduce() method is used to count the frequency of the elements of the array. We are printing the frequency of “1” in the array in the console.

Javascript




const arr = [1, 2, 3, 2, 3, 4, 5,
             5, 6, 1, 1, 4, 5, 7, 8, 8];
 
function count(arr, element) {
    return arr.reduce((ele, arrayEle) =>
        (arrayEle == element ? ele + 1 : ele), 0);
};
 
console.log(count(arr, 1));


Output:

3

Approach 4: Using for-of loop: 

The for…of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …,etc), invoking a custom iteration hook with statements to be executed for the value of each distinct property.

Syntax:

for ( variable of iterableObjectName) {
   ...
}

Example: In this example, the for loop iterates over the array of elements and prints the occurrences of all the elements in the console.

Javascript




const arr = [1, 2, 3, 2, 3, 4, 5,
             5, 6, 1, 1, 4, 5, 7, 8, 8];
 
const count = {};
 
for (let ele of arr) {
    if (count[ele]) {
        count[ele] += 1;
    } else {
        count[ele] = 1;
    }
}
console.log(count);


Output:

{ '1': 3, '2': 2, '3': 2, '4': 2, '5': 3, '6': 1, '7': 1, '8': 2 }


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads