[JavaScript] how to find most repeated element from an array?
I want to find most repeated element from an array, how can I do that in Javascript?
1 Answer
4 years ago by Jahaan
Below code shows you how to find the most repeated element preset in an array using ES6 reduce function.
let numbers = [1, 22, 35, 22, 1 , 5, 22, 5];
const mostRepeatedElement = numbers.reduce((previous, current, i, arr) => arr.filter(ele => ele === previous).length > arr.filter(ele => ele === current).length ? previous : current);
console.log("Most Repeated value :", mostRepeatedElement); // Outputs 22
4 years ago by Meera