check frequency of string in array js

Solutions on MaxInterview for check frequency of string in array js by the best coders in the world

showing results for - "check frequency of string in array js"
Sara
17 Oct 2016
1var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
2var counts = {};
3
4for (var i = 0; i < arr.length; i++) {
5  var num = arr[i];
6  counts[num] = counts[num] ? counts[num] + 1 : 1;
7}
8
9console.log(counts[5], counts[2], counts[9], counts[4]);