Underscore.js: Find the most frequently occurring value in an array?

You can do this in one pass using _.reduce. The basic idea is to keep track of the word frequencies and the most common word at the same time:

var o = _(foods).reduce(function(o, s) {
    o.freq[s] = (o.freq[s] || 0) + 1;
    if(!o.freq[o.most] || o.freq[s] > o.freq[o.most])
        o.most = s;
    return o;
}, { freq: { }, most: '' });

That leaves 'hotdot' in o.most.

Demo: http://jsfiddle.net/ambiguous/G9W4m/

You can also do it with each (or even a simple for loop) if you don't mind predeclaring the cache variable:

var o = { freq: { }, most: '' };
_(foods).each(function(s) {
    o.freq[s] = (o.freq[s] || 0) + 1;
    if(!o.freq[o.most] || o.freq[s] > o.freq[o.most])
        o.most = s;
});

Demo: http://jsfiddle.net/ambiguous/WvXEV/

You could also break o into two pieces and use a slightly modified version of the above, then you wouldn't have to say o.most to get 'hotdog'.


var foods = ['hotdog', 'hamburger', 'soup', 'sandwich', 'hotdog', 'watermelon', 'hotdog'];
var result = _.chain(foods).countBy().pairs().max(_.last).head().value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

countBy: Sorts a list into groups and returns a count for the number of objects in each group.

pairs: Convert an object into a list of [key, value] pairs.

max: Returns the maximum value in list. If an iterator function is provided, it will be used on each value to generate the criterion by which the value is ranked.

last: Returns the last element of an array

head: Returns the first element of an array

chain: Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is used.

value: Extracts the value of a wrapped object.