

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Find the most frequent number in the array and how many times it is repeated in JavaScript
We are required to write a JavaScript function that takes in an array of literals and finds the most frequent number in the array and how many times it is repeated.
Example
The code for this will be −
const arr = ['13', '4', '1', '1', '4', '2', '3', '4', '4', '1', '2', '4', '9', '3']; const findFrequency = (arr = []) => { const count = {}; const max = arr.reduce((acc, val, ind) => { count[val] = (count[val] || 0) + 1; if (!ind || count[val] > count[acc[0]]) { return [val]; }; if (val !== acc[0] && count[val] === count[acc[0]]) { acc.push(val); }; return acc; }, undefined); return { max, count }; } console.log(findFrequency(arr));
Output
And the output in the console will be −
{ max: [ '4' ], count: { '1': 3, '2': 2, '3': 2, '4': 5, '9': 1, '13': 1 } }
- Related Questions & Answers
- Find the second most frequent element in array JavaScript
- Find Second most frequent character in array - JavaScript
- Finding the most frequent word(s) in an array using JavaScript
- Finding the second most frequent character in JavaScript
- Return the element that appears for second most number of times in the array JavaScript
- C# program to find the most frequent element
- Get the item that appears the most times in an array JavaScript
- Most Frequent Number in Intervals in C++
- How many times can we sum number digits in JavaScript
- Finding number of occurrences of the element appearing most number of times in JavaScript
- Is element repeated more than n times in JavaScript
- Most frequent element in an array in C++
- Find the k most frequent words from data set in Python
- Program to find frequency of the most frequent element in Python
- Program to find out the index of the most frequent element in a concealed array in Python
Advertisements