Answers for "check frequency of string in array js"

0

check frequency of string in array js

var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
var counts = {};

for (var i = 0; i < arr.length; i++) {
  var num = arr[i];
  counts[num] = counts[num] ? counts[num] + 1 : 1;
}

console.log(counts[5], counts[2], counts[9], counts[4]);
Posted by: Guest on June-09-2021

Code answers related to "check frequency of string in array js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language