how to count occurrences of each element in an array in javascript