Most Frequent Element in an Array using JavaScript
We will demonstrate different methods to find the most frequent element in an array in JavaScript. We will be given an array with multiple occurrences of values in that array and we have to output the maximum occurred element in that array.
Methods to Find the most frequent element in an array in JavaScript
Table of Content
- Naive method using Loops and Sort function
- Using JavaScript Object
- Using JavaScript Map and forEach method
- Using filter() and reduce()
- Using reduce() and a Frequency Map
Naive method using Loops and Sort function
In this method, we will sort the given array and traverse it to find the maximum occurred element.
Example:
arr = [
1, 1, 3, 5, 7, 6, 8, 5, 6, 4,
7, 6, 0, 2, 1, 6, 8, 9, 5,
];
arr.sort((a, b) => a - b);
//console.log(arr)
let count = 1,
max = 0,
el;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] === arr[i - 1]) {
count++;
} else {
count = 1;
}
if (count > max) {
max = count;
el = arr[i];
}
}
console.log("The most occured element is: " + el);
Output
The most occured element is: 6
Using JavaScript Object
In this approach we will use JavaScript object to store the number and their occurences and find the element occurred most frequently.
Example:
const arr = [
1, 1, 3, 5, 7, 6, 8, 5, 6, 4,
7, 6, 0, 2, 1, 6, 8, 9, 5,
];
obj = {};
let el,
max = 0;
for (let i = 0; i < arr.length; i++) {
if (!obj[arr[i]]) obj[arr[i]] = 1;
else obj[arr[i]]++;
}
for (const i in obj) {
if (max < obj[i]) {
max = obj[i];
el = i;
}
}
console.log("The most occured element is: " + el);
Output
The most occured element is: 6
Using JavaScript Map and forEach method
In this method, we will use JavaScript Map to map the values with number of occurrences and print the maximum occurred element
Example:
const arr = [
3, 5, 7, 6, 8, 5, 6, 4, 7, 1,
6, 0, 2, 1, 6, 8, 9, 5, 1,
];
let m = new Map();
for (const i in arr) {
if (!m.get(arr[i])) m.set(arr[i], 1);
else {
m.set(arr[i], m.get(arr[i]) + 1);
}
}
let max = 0;
let el;
m.forEach((val, key, map) => {
if (max < val) {
max = val;
el = key;
}
});
console.log("The most occured element is: " + el);
Output
The most occured element is: 6
Using filter() and reduce()
Using filter() to get unique elements, then reduce() to find the most frequent one by comparing filtered counts. This method efficiently determines the most frequent element in an array.
Example:
const array = [1, 2, 3, 2, 2, 3, 1, 4, 2];
const mostFrequent = Array.from(new Set(array)).reduce((prev, curr) =>
array.filter(el => el === curr).length > array.filter(el => el === prev).length ? curr : prev
);
console.log(mostFrequent);
Output
2
Using reduce() and a Frequency Map
In this approach, we use the reduce() function to build a frequency map of the elements in the array. Then, we find the element with the highest frequency by iterating through this map.
Example:
const arr = [
1, 1, 3, 5, 7, 6, 8, 5, 6, 4,
7, 6, 0, 2, 1, 6, 8, 9, 5,
];
// Create a frequency map using reduce
const frequencyMap = arr.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});
// Find the most frequent element
let maxCount = 0;
let mostFrequentElement;
for (const [element, count] of Object.entries(frequencyMap)) {
if (count > maxCount) {
maxCount = count;
mostFrequentElement = element;
}
}
console.log("The most occurred element is: " + mostFrequentElement);
Output
The most occurred element is: 6
Contact Us