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
JavaScript array: Find all elements that appear more than n times
In JavaScript, you can find all elements that appear more than n times in an array using various approaches. This is useful for data analysis, finding duplicates, or filtering frequent items.
Problem Overview
Given an array of numbers or strings with repeated entries, we need to write a function that takes a positive integer n and returns all elements that appear more than or equal to n times.
Using Map() for Frequency Counting
The most efficient approach uses a Map to track element frequencies, then filters elements that meet our criteria:
const arr = [34, 6, 34, 8, 54, 7, 87, 23, 34, 6, 21, 6, 23, 4, 23];
const findFrequentElements = (arr, n) => {
// Count frequencies using Map
const frequencyMap = new Map();
arr.forEach(element => {
frequencyMap.set(element, (frequencyMap.get(element) || 0) + 1);
});
// Find elements with frequency >= n
const result = [];
frequencyMap.forEach((count, element) => {
if (count >= n) {
result.push(element);
}
});
return result;
};
console.log(findFrequentElements(arr, 3));
console.log(findFrequentElements(arr, 2));
[ 34, 6, 23 ] [ 34, 6, 23 ]
Using reduce() Method
A more concise approach using reduce() to count and filter in one pass:
const findFrequentElementsReduce = (arr, n) => {
const frequencies = arr.reduce((acc, val) => {
acc[val] = (acc[val] || 0) + 1;
return acc;
}, {});
return Object.keys(frequencies)
.filter(key => frequencies[key] >= n)
.map(key => isNaN(key) ? key : Number(key));
};
const testArray = [34, 6, 34, 8, 54, 7, 87, 23, 34, 6, 21, 6, 23, 4, 23];
console.log(findFrequentElementsReduce(testArray, 3));
[ 34, 6, 23 ]
Working with String Arrays
The same approach works perfectly with string arrays:
const words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple', 'grape'];
const findFrequentWords = (arr, n) => {
const frequencies = new Map();
arr.forEach(word => {
frequencies.set(word, (frequencies.get(word) || 0) + 1);
});
return [...frequencies.entries()]
.filter(([word, count]) => count >= n)
.map(([word, count]) => word);
};
console.log(findFrequentWords(words, 2));
console.log(findFrequentWords(words, 3));
[ 'apple', 'banana' ] [ 'apple' ]
Comparison of Methods
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Map() approach | O(n) | O(k) | High |
| reduce() approach | O(n) | O(k) | Medium |
Note: n = array length, k = number of unique elements
Practical Example
Here's a complete example that handles edge cases:
const findElementsAppearingNTimes = (arr, n) => {
if (!Array.isArray(arr) || n <= 0) {
return [];
}
const frequencies = new Map();
// Count frequencies
arr.forEach(element => {
frequencies.set(element, (frequencies.get(element) || 0) + 1);
});
// Return elements that appear >= n times
return Array.from(frequencies.entries())
.filter(([element, count]) => count >= n)
.map(([element, count]) => element);
};
// Test with different scenarios
console.log(findElementsAppearingNTimes([1, 2, 2, 3, 3, 3], 2)); // Elements appearing 2+ times
console.log(findElementsAppearingNTimes([1, 1, 1, 1], 3)); // Elements appearing 3+ times
console.log(findElementsAppearingNTimes([], 1)); // Empty array
[ 2, 3 ] [ 1 ] []
Conclusion
Use Map() for the cleanest and most efficient solution to find frequent elements. This approach offers O(n) time complexity and handles both numeric and string arrays effectively.
