Count Frequency of an Array Item in JavaScript Last Updated : 14 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Here are the different approaches to count the frequency of an Array Item in JavaScriptUsing a Loop and CounterThis is the most basic and efficient approach when you want to find the frequency of a single item. You simply loop through the array and count how many times the item appears. JavaScript const frequency = (arr, item) => { let count = 0; for (let i = 0; i < arr.length; i++) { if (arr[i] === item) { count++; } } return count; }; const a = [1, 2, 3, 2, 1, 2, 3, 1]; console.log(frequency(a, 2)); Output3Using filter() MethodYou can filter the array to only include occurrences of the target element and then return the length of the filtered array. JavaScript const frequency = (arr, item) => { return arr.filter(x => x === item).length; }; const a = [1, 2, 3, 2, 1, 2, 3, 1]; console.log(frequency(a, 2)); Output3Using reduce() MethodThe reduce() method can also be used to accumulate the frequency of a single item by checking each element in the array. JavaScript const frequency = (arr, item) => { return arr.reduce((count, x) => x === item ? count + 1 : count, 0); }; const a = [1, 2, 3, 2, 1, 2, 3, 1]; console.log(frequency(a, 2)); Output3 Using an Object (for Larger Arrays)If you're working with larger arrays and you want to count occurrences efficiently, an object can be used to store counts of all items. But you can optimize it for a single item by simply counting the target item. JavaScript const frequency = (a, item) => { const o = {}; a.forEach(x => { if (x === item) { o[item] = (o[item] || 0) + 1; } }); return o[item] || 0; }; const a = [1, 2, 3, 2, 1, 2, 3, 1]; console.log(frequency(a, 2)); Output3 Count the frequency of elements in an Array Visit Course Count the frequency of elements in an Array Count Frequency of an Array Item in JavaScript Comment More infoAdvertise with us Next Article How to Get the Size of an Array in JavaScript A amitsingh2730 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-DSA Similar Reads JavaScript- Count Frequencies in an Array These are the following ways to count the frequency:1. Using Object (Efficient for Small to Moderate Arrays)We use JavaScript Objects to store frequencies, we iterate over the array using the forEach() method. For each element, we check if the element already exists as a key in the res object. If it 2 min read JavaScript - Counting Frequencies of Array Elements Here are the various approaches to count the frequencies of array elements in JavaScript.Using an Object (Simple and Efficient)This is the most common approach for counting frequency in an array. Each array element becomes a key in the object, and its value is incremented as the element appears.Java 2 min read Count Occurrences of All Items in an Array in JavaScript Here are the different methods to count the occurrences of all items in an array in JavaScript1. Using forEach() MethodThe arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array. 3 min read How to count array items in AngularJS ? Given an array and the task is to get the length of an array variable in AngularJS. For this, we will be using the .length() method to get the length of an array variable. Syntax: array.length();Example 1: In this example, the array length is shown by the alert box. Here, we have an array containing 2 min read How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that 2 min read How to Compute the Sum and Average of Elements in an Array in JavaScript? Computing the sum and average of elements in an array involves iterating through the array, accumulating the sum of its elements, and then dividing the sum by the total number of elements to get the average. This can be achieved using various techniques, from basic loops to more advanced functional 3 min read Like