Count occurrences in array given values of another array

http://stackoverflow.com/questions/26102879/count-occurrences-in-array-given-values-of-another-array

by austinpray

JavaScript

// names we want to find
var names = ["john", "pete", "paul"];

// target list of names.
// john, pete, paul appear 2x
var target = ["john", "pete", "jack", "cindy", "thomas", "paul", "john", "pete", "paul"];

function finder(search, target) {
    return search.map(function (val) {
        return target.filter(function (e) {
            return val === e;
        }).length;
    });
}

finder(names, target);
// => [2, 2, 2]

console.log(finder(names,target));