Frequency counter Pattern in Data structures and algorithms with coding exercises and solutions

Frequency counter Pattern in Data structures and algorithms with coding exercises and solutions

In this article, we will learn and practice frequency counter pattern questions.

Introduction :-

what is frequency counter pattern ?

  • This pattern uses object or sets of collected values / frequencies of value.

  • These patterns avoid the need of nested loops i.e. O(n^2) operations with array/strings.

Coding exercises (try to do it yourself ............ :)

1. Write a function called sameFrequency, which accepts two arrays. The function should return true if every value in the array has it's corresponding value squared in the second array. The frequency of values must be the same.

ex:-

  • sameFrequency([1, 2, 3], [4, 1, 9]); // true
  • sameFrequency([1, 2, 3], [1, 9]); // false
  • sameFrequency([1, 2, 1], [4, 4, 1]); // false (must be same frequency)

solution:-

function sameFrequency(arr1,arr2){

let freqCount1={}
let freqCount2={}

if(arr1.length!==arr2.length){
  return false
}

for(let i=0;i<arr1.length;i++){
  let currIndex=arr1[i]

  freqCount1[currIndex]?freqCount1[currIndex]+=1:freqCount1[currIndex]=1
}

for(let i=0;i<arr2.length;i++){
  let currIndex=arr2[i]

  freqCount2[currIndex]?freqCount2[currIndex]+=1:freqCount2[currIndex]=1
}

for (let key in freqCount1) {
    if (!(key ** 2 in freqCount2)) return false;
    if (freqCount1[key] !== freqCount2[key ** 2]) return false;
  }  

 return true;

}

2. Anagrams

Given two strings write a function to find out if the other string is anagram of first . Anagrams are rearranged words in different sequential manner. return true if anagram else return false ex:-

  • isAnagram('anagram','nagaram') //true
  • isAnagram(''aab,'bca') //false

solution

function isAnagram(str1,str2){

  if(str1.length!==str2.length){
    return false;
  }

  let lookup={}

  for(let i=0;i<str1.length;i++){
    let currIndex=str1[i]
    lookup[currIndex]?lookup[currIndex]+=1:lookup[currIndex]=1
  }

  for(let i=0;i<str2.length;i++){
    let currIndex=str2[i]

    if(!lookup[currIndex]){
      return false
    }
    lookup[currIndex]-=1
  }

  return true
}

3. Write a function called same . Given two positive integers, find out if the two numbers have the same frequency of digits.

ex:-

  • same(182, 281) //true
  • same(34,14) //false

solution

const same = (val1, val2) ={
  const str1 = val1.toString();
  const str2 = val2.toString();

  if (str1.length !== str2.length) {
    return false;
  }

  let store = {};

  for (let i = 0; i < str1.length; i++) {
    let counter = str1[i];
    if (store[counter]) {
      store[counter]++;
    } else {
      store[counter] = 1;
    }
  }
  for (let i = 0; i < str2.length; i++) {
    let counter = str2[i];
    if (!store[counter]) {
      return false;
    }
    store[counter]--;
  }
  return true;
};

4. Any duplicate implement a function anyDuplicate which accepts variable number of arguments. and checks if there are any duplicates amongst arguments.

ex:-

  • anyDuplicate(1, 2, 2, 3) //true
  • anyDuplicate(1, 2, 3) //false

solution

function anyDuplicate(...arguments){

  let lookup={}

  for(i=0;i<arguments.length;i++){
    let index= arguments[i];
    lookup[index]? lookup[index]+=1: lookup[index]=1
  }

  for(let key in lookup){
    if(lookup[key]>1)
      return true
  }

 return false 
}

5. Sort Array by Increasing Frequency Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.

Return the sorted array.

ex:- Input: nums = [1,1,2,2,2,3] Output: [3,1,1,2,2,2] Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.

solution

var frequencySort = function(nums) {

    let lookup={};

    for(let i=0;i<nums.length;i++){
        let index= nums[i]

        lookup[index]?lookup[index]+=1:lookup[index]=1;
    }

    console.log(lookup)

    return nums.sort((a,b) => lookup[a] - lookup[b] || b - a);


};

6. Find Lucky Integer in an Array Given an array of integers arr, a lucky integer is an integer that has a frequency in the array equal to
its value.

Return the largest lucky integer in the array. If there is no lucky integer return -1.

ex:- Input: arr = [1,2,2,3,3,3] Output: 3 Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.

solution:-

var findLucky = function(arr) {

    let obj={}
    let storeArray=[];

    for(let i=0;i<arr.length;i++){
      let currIndex= arr[i]
      obj[currIndex]?obj[currIndex]+=1:obj[currIndex]=1;
    }
    console.log(obj)
    for(let key in obj){
      if(key==obj[key])
      {
        storeArray.push(parseInt(key))
      }
    }

    let max=-1;

    for(let i=0;i<storeArray.length;i++){
      if(storeArray[i]>max){
        max=storeArray[i];
      }
    }
    return max
};

Conclusion :-

So we have learned and practiced how to solve frequency counter pattern questions.

Special thanks to Colt Steele's course and leetcode for the practice questions.

That’s it and if you found this article helpful, please hit the like button 👍 or react ❤️, you can also follow me on hashnode and if you have any doubt, feel free to comment! I’d be happy to help :)