Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Highest occurrence in an array or first selected in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. Our function should then return the highest occurrence of an array value, and if there's an equal occurrence, we should return the first selected value of the equal occurrences.
Problem Statement
Given an array of values, find the element that appears most frequently. If multiple elements have the same highest frequency, return the one that appears first in the array.
const arr1 = ['25', '50', 'a', 'a', 'b', 'c']; // 'a' appears 2 times (most frequent), so return 'a' const arr2 = ['75', '100', 'a', 'b', 'b', 'a']; // Both 'a' and 'b' appear 2 times, but 'a' appears first, so return 'a'
Solution
We'll use an object to track the count and first occurrence index of each element:
const arr = ['25', '50', 'a', 'a', 'b', 'c'];
const arr1 = ['75', '100', 'a', 'b', 'b', 'a'];
const getMostFrequentValue = (arr = []) => {
let count = 0, ind = -1;
arr.forEach((el, i) => {
this[el] = this[el] || { count: 0, ind: i };
this[el].count++;
if (this[el].count > count) {
count = this[el].count;
ind = this[el].ind;
return;
}
if (this[el].count === count && this[el].ind < ind) {
ind = this[el].ind;
}
}, Object.create(null));
return arr[ind];
};
console.log(getMostFrequentValue(arr)); // First array
console.log(getMostFrequentValue(arr1)); // Second array
a a
Alternative Approach Using Map
Here's a cleaner implementation using Map for better readability:
const getMostFrequentValueWithMap = (arr = []) => {
const frequencyMap = new Map();
let maxCount = 0;
let result = arr[0];
let resultIndex = 0;
arr.forEach((element, index) => {
const count = (frequencyMap.get(element) || 0) + 1;
frequencyMap.set(element, count);
if (count > maxCount || (count === maxCount && index < resultIndex)) {
maxCount = count;
result = element;
resultIndex = index;
}
});
return result;
};
const testArr1 = ['x', 'y', 'x', 'z', 'y', 'x'];
const testArr2 = ['p', 'q', 'r', 'q', 'p'];
console.log(getMostFrequentValueWithMap(testArr1)); // 'x' (appears 3 times)
console.log(getMostFrequentValueWithMap(testArr2)); // 'p' (appears first among tied elements)
x p
How It Works
The algorithm works by:
- Tracking the frequency of each element and its first occurrence index
- Updating the result when finding a higher frequency
- When frequencies are equal, preferring the element that appeared first
Comparison
| Approach | Readability | Performance | Memory Usage |
|---|---|---|---|
| Object with forEach | Complex | O(n) | O(n) |
| Map-based | Clear | O(n) | O(n) |
Conclusion
Both approaches solve the problem efficiently in O(n) time. The Map-based solution is more readable and easier to understand, making it the preferred choice for most scenarios.
