The FrequencyCount function count how many times each element appear in the array. It creates empty object freq, iterate through the array.
Program in JavaScript:
Javascript Program :
function frequencyCount(arr){
let freq={};
for(let i=0;i<arr.length;i++)
{
if(freq[arr[i]])
{
freq[arr[i]]+= 1;
}
else{
freq[arr[i]]=1;
}
}
return freq;
}
console.log(frequencyCount([1,2,3,1,2,3]));
Output: {"1":2,"2":2,"3":2}
No comments:
Post a Comment