JavaScript Program to Find the Most Frequently Occurring Element in an Array Last Updated : 04 Jun, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn how to find the most frequently occurring element in an array we can use objects to count the occurrences of each element in an array. Methods to find the most frequently occurring element in an array in JavaScript: Table of Content Method 1: Using the reduce() methodMethod 2: Using the JavaScript Map()Method 3: Using the Math.max methodMethod 4: Using sortingMethod 1: Using the reduce() methodTo find the highest count of elements we can use the "reduce" method. Example: This example shows the use of the above-explained approach. JavaScript function mostFrequentUsingReduce(arr) { const counts = arr.reduce((acc, num) => { acc[num] = (acc[num] || 0) + 1; return acc; }, {}); return Object.keys(counts).reduce((a, b) => (counts[a] > counts[b] ? a : b)); } const array = ['Cat', 'Dog', 'Cat', 'Goat', 'Cow', 'Cat', ]; console.log(mostFrequentUsingReduce(array)); OutputCat Method 2: Using the JavaScript Map()By using the "Map" Data Structure we can store the occurrence of elements in an array. Example:This example shows the use of the above-explained approach. JavaScript function mostFrequentUsingMap(arr) { const counts = new Map(); for (let num of arr) { counts.set(num, (counts.get(num) || 0) + 1); } let mostFrequent; let maxCount = 0; counts.forEach((count, num) => { if (count > maxCount) { maxCount = count; mostFrequent = num; } }); return mostFrequent; } const array = [1, 2, 2, 3, 2, 3, 4]; console.log(mostFrequentUsingMap(array)); Output2 Method 3: Using the Math.max methodBy using "Math.max" also we can find the highest frequency of given array elements. Example: This example shows the use of the above-explained approach. JavaScript function mostFrequentUsingMathMax(arr) { const counts = {}; for (let num of arr) { counts[num] = (counts[num] || 0) + 1; } const maxCount = Math.max(...Object.values(counts)); const mostFrequent = Object.keys(counts).find(key => counts[key] === maxCount); return mostFrequent; } const array = [1, 5, 2, 3, 5, 6, 4]; console.log(mostFrequentUsingMathMax(array)); Output5 Method 4: Using sortingThis approach sorts the array, then iterates through it, counting consecutive occurrences of each element. It keeps track of the most frequent element encountered so far and updates it if a new element becomes more frequent. Finally, it returns the most frequent element found. JavaScript function mostFrequentUsingSorting(arr) { arr.sort(); // Sort the array to group same elements together let maxCount = 0; let mostFrequent; let currentCount = 1; for (let i = 1; i < arr.length; i++) { if (arr[i] === arr[i - 1]) { currentCount++; } else { if (currentCount > maxCount) { maxCount = currentCount; mostFrequent = arr[i - 1]; } currentCount = 1; // Reset current count } } // Check for the last element if (currentCount > maxCount) { maxCount = currentCount; mostFrequent = arr[arr.length - 1]; } return mostFrequent; } // Example usage const array = ['Cat', 'Dog', 'Cat', 'Goat', 'Cow', 'Cat']; console.log(mostFrequentUsingSorting(array)); // Output: Cat OutputCat Comment More infoAdvertise with us Next Article JavaScript - Most Frequent Element in an Array G geethabtrweh Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Program +1 More Similar Reads JavaScript Program to Find k Most Frequent Elements in Array In this article, we are given an input array with the elements and k value. Our task is to find out the most frequent elements in the array as per the k value using JavaScript. Below we have added the example for better understanding. Example: Input: array = [7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9] , K 3 min read JavaScript Program to Find k Most Occurrences in the Given Array K most occurrences in an array refer to finding the K unique elements that appear the most frequently within the given array, where K is a specified integer. These are the elements with the highest frequencies in the array.Example:Input: arr[] = {3, 1, 4, 4, 5, 2, 6, 1}, K = 2Output: 4 1Explanation: 6 min read JavaScript Program for Finding the Majority Element of an Array Finding the majority element in an array is a common problem in computer science and data analysis. The majority element in an array is the element that appears more than n/2 times, where n is the length of the array. In other words, it's the element that occurs more frequently than any other elemen 3 min read JavaScript - Most Frequent Element in an Array Let us talk about different methods to find the most frequent element in an array in JavaScript.Using JavaScript Object (For Small to Moderate Arrays)In this approach we use JavaScript object to store the number and their occurrences and find the element occurred most frequently.JavaScriptfunction m 3 min read JavaScript Program to Find all Elements that Appear More than n/k Times We are given an array that has n number of elements and an integer k, we have to return all the elements which will appear more than n/k times in the array using JavaScript. Examples:Input: arr = {4, 2, 4, 1, 4, 5, 5, 5}, k=3Output: {4 , 5}Explanation: The elements {4, 5} appears more than n/k i.e. 3 min read JavaScript Program to Find the Majority Element that occurs more than N/2 Times We are given a JavaScript array and we need to find out the majority element in it which occurs more than N/2 times where N is the size of the array. Example:Input: arr[] = {3, 5, 8, 5, 4, 5, 7, 5, 5};Output: 5Explanation: Here, N = 9 and N/2 = 4, 5 appears 5 times in the array which is more than th 3 min read Like