es6 print word and count from array code example
Example 1: word count algorithm javascript
function getWordCount() { let map = {};for (let i = 0; i < array.length; i++) { let item = array[i]; map[item] = (map[item] + 1) || 1; } return map;}getWordCount(array);
Example 2: javascript Count the occurrences of a value in an array
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
countOccurrences([2, 1, 3, 3, 2, 3], 2);
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b'], 'a');