Count Frequency of an Array Item in JavaScript Last Updated : 14 Nov, 2024 Comments Improve Suggest changes 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 Comment More infoAdvertise with us Next Article Count Frequency of an Array Item 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 JavaScript - Find an Item in an Array Here are the various methods to find an item in an array in JavaScript1. Using the includes() methodThe includes() method checks if an array includes a certain value, returning the boolean value true or false accordingly.JavaScriptconst a = ['apple', 'banana', 'orange']; console.log(a.includes('bana 4 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read JavaScript - Find Index of a Value in Array Here are some effective methods to find the array index with a value in JavaScript.Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. JavaScriptconst a = [10, 20, 30, 40, 50]; // Find index of value 30 const index = a.inde 2 min read JavaScript - Frequency of characters in Order of Appearance The operation involves counting how many times each character in alphabetic characters in a given string.Using Object (Simple and Efficient for Small to Moderate Strings)Object in JavaScript allow storing key value pairs. So we use items as keys and their frequencies as values.JavaScriptfunction fre 2 min read Javascript Program for Least frequent element in an array Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them.Examples : Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1}Output : 33 appears minimum number of times in givenarray.Input : arr[] = {10, 20, 30}Output : 10 or 20 or 3 min read How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b 3 min read JavaScript - Get first and last Item of JS Array These are the following ways to get first and last element of the given array: 1. Using length PropertyThe array length property in JavaScript is used to set or return the number of elements in an array. JavaScriptlet arr = [3, 2, 3, 4, 5]; let f = arr[0]; let l = arr[arr.length - 1]; console.log(f, 2 min read Like