How to make a frequency counter algorithm

frequency_counter _algorithm

The frequency counter algorithm measures the occurrence of a value in data. It uses objects or sets to collects the value/frequency of data.

Frequency counter algorithm example javascript

Write a function called frequencyCounter, which accepts a string. The function should return the frequency of each character in the given string.

Frequency counter pseudocode

  • Write a function that accepts a string.
  • Declare an empty object to hold your frequency.
  • Loop over the the given string with K = 0 and less than the length of the string.
  • Check if the object declared already have the value in loop, increment object value by 1.
  • Otherwise set object value = 1.
  • Finally return the object.

Write a function that accepts a string.

const frequencyCounter = (str) => {

}

Declare an empty object to hold your frequency.

const frequencyCounter = (str) => {
    let textFrequency  = {}
}

Loop over the given string

const frequencyCounter = (str) => {
    let textFrequency  = {}

    for (let i = 0; i < str.length; i++) {
        const element = str[i];

    }
}

Check if the object declared already has the element in the loop, increment by 1 or set by 1.

const frequencyCounter = (str) => {
    let textFrequency  = {}

    for (let i = 0; i < str.length; i++) {
        const element = str[i];

        if(textFrequency[element]){
            textFrequency[element] += 1;
        }else{
            textFrequency[element] = 1
        }

    }
}

Finally return the frequency object.

const frequencyCounter = (str) => {
    let textFrequency  = {}

    for (let i = 0; i < str.length; i++) {
        const element = str[i];

        if(textFrequency[element]){
            textFrequency[element] += 1;
        }else{
            textFrequency[element] = 1
        }
    }
return textFrequency;
}

console.log(frequencyCounter('madam'));  //  { m: 2, a: 2, d: 1 }
const frequencyCounter = (str) => {
    let textFrequency  = {}

    for (let i = 0; i < str.length; i++) {
        const element = str[i];

        if(textFrequency[element]){
            textFrequency[element] += 1;
        }else{
            textFrequency[element] = 1
        }
    }
return textFrequency;
}

console.log(frequencyCounter(' Pseudopseudohypoparathyroidism'));  

*/
{
  P: 1,
  s: 3,
  e: 2,
  u: 2,
  d: 3,
  o: 4,
  p: 3,
  h: 2,
  y: 2,
  a: 2,
  r: 2,
  t: 1,
  i: 2,
  m: 1
} /*

Below is a code example I wrote removing multiple users from a list using a frequency counter.

const getLeaderBorad = async (req, res) => {
    await GameRecord.find({correct_qst : 15}).sort({minutes: 1}).sort({seconds: 1}).limit(50).then((leaders)=> {
        // console.log(leaders);
        let uniqueCount = {};
        let uniqueLeader = []
        for (let i = 0; i < leaders.length; i++) {
            const element = leaders[i];
         
            if(uniqueCount[element.user] == element.user){
            }else{
                uniqueCount[element.user] = element.user;
                uniqueLeader.push(element);
            }
            
        }
       
        res.status(200).send({ document : uniqueLeader});
    })
   
}

With the given example in this article, you should identify where the frequency counter algorithm is used.

You can also view other algorithms examples.

If you want to improve your skill in algorithms, use any of the links below.

If you find this helpful share.

Leave a Reply

Your email address will not be published. Required fields are marked *