Skip to content
Advertisement

Get the element with the highest occurrence in an array

I’m looking for an elegant way of determining which element has the highest occurrence (mode) in a JavaScript array.

For example, in

JavaScript

the 'apple' element is the most frequent one.

Advertisement

Answer

This is just the mode. Here’s a quick, non-optimized solution. It should be O(n).

JavaScript
Advertisement