
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Number of Occurrences of the Most Frequent Element in JavaScript
We are required to write a JavaScript function that takes in an array of literals and returns the count of elements that appears for the most number of times in the array.
Example
The code for this will be −
let arr = [2, 8, 4, 8, 6, 4, 7, 8]; const countOccurence = arr => { const max = arr.reduce((acc, val) => { return Math.max(acc, val); }, -Infinity); const count = arr.filter(el => { return el === max; }); const { length } = count; return length; }; console.log(countOccurence(arr));
Output
The output in the console −
3
Advertisements