var collectionRepeat = function(box, key){
    var counter = {};
  
    box.forEach(function(x) { 
        counter[x] = (counter[x] || 0) + 1; 
    });
  
    var val = counter[key];

    if (key === undefined) {
        return counter;
    }
  
    return (val) === undefined ? 0 : val;
}

var ary = ['a', 'b', 'a', 'c', 'b', 'd', 'a', 'e'];
console.log(collectionRepeat(ary)); // Object { a: 3, b: 2, c: 1, d: 1, e: 1 }
console.log(collectionRepeat(ary, 'a')); // => 3
console.log(collectionRepeat(ary, 'b')); // => 2 
console.log(collectionRepeat(ary, 'c')); // => 1 
console.log(collectionRepeat(ary, 'z')); // => 0 
Rerun