How to print frequency count of element in the array ? - Free Web Development || UI Development || React JS || Angular

Latest

Free Web Development ||Free UI Development || React JS || HTML5 || CSS3 || JavaScript || Angular Learn Web Development || Frontend Developer || Full Stack Developer

1 / 3
Welcome to Shravan Ghanchi

Wednesday, April 23, 2025

How to print frequency count of element in the array ?

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

Snow
Forest
Mountains
Snow
Forest
Mountains