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:
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 }
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
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
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 }
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
28 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...