
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Get greatest repetitive item in array JavaScript
We have an array of Number / String literals that contains some values (some are repetitive as well). Our job is to write a function that returns the element from the array which appears for the greatest number of times in the array.
For example − if the input array is −
const input = ['a', 'v', 'k', 'f', 'a', 'f', 's', 'd', 'd', 'f', 'a', 'j', 'a'];
Then the output should be −
'a'
because 'a' gets repeated for the maximum number of times
Therefore, let’s write the code for this. We will use a Map() to keep track of all the elements we encounter and their count, and at last return the element with maximum count like this −
Example
const input = ['m', 'a', 'v', 'k', 'f', 'a', 'f', 's', 'd', 'd', 'f', 'a', 'j', 'a']; const findMaximum = arr => { const map = arr.reduce((acc, val) => { let count = acc.get(val); if(count){ acc.set(val, ++count); } else { acc.set(val, 1); }; return acc; }, new Map()); return Array.from(map).reduce((acc, val) => { if(val[1] > acc[1]){ return val; }; return acc; }, [0, 0])[0]; }; console.log(findMaximum(input));
Output
The output in the console will be −
a
- Related Articles
- Get unique item from two different array in JavaScript
- Get the first and last item in an array using JavaScript?
- Get the item that appears the most times in an array JavaScript
- Get the index of the nth item of a type in a JavaScript array
- Greatest element in a Multi-Dimensional Array in JavaScript
- Greatest number in a dynamically typed array in JavaScript
- How to splice duplicate item in array JavaScript
- Twice repetitive word count in a string - JavaScript
- Finding a greatest number in a nested array in JavaScript
- Return indexes of greatest values in an array in JavaScript
- Constructing an object from repetitive numeral string in JavaScript
- Find first duplicate item in array in linear time JavaScript
- Remove item from a nested array by indices in JavaScript
- How to sort array by first item in subarray - JavaScript?
- Finding intersection of arrays that contain repetitive entries in JavaScript

Advertisements