How to Compare Arrays in JavaScript || Efficient Frequency Counter Method

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.ย. 2024
  • I’m going to show you two ways of solving a typical interview-style question where we need to compare two arrays. The first solution is more obvious and less efficient. The second solution introduces a great problem-solving tool - frequency counter objects - which greatly improves the efficiency of the function.
    The problem:
    “Write a function called “squared” which takes two arrays. The function should return true if every value in the array has its value squared in the second array. The frequency of values must be the same.”
    At first, I will show you the “Naïve” way of solving the problem - the more obvious way that isn’t efficient. I’ll then show you an efficient way to solve the problem using “frequency counter objects”. This is a very handy technique to have in your problem-solving toolbox.

ความคิดเห็น • 7

  • @marvinsequera
    @marvinsequera 3 ปีที่แล้ว +5

    A friend sent me your article, I just came here to say good work.

    • @doabledanny
      @doabledanny  3 ปีที่แล้ว

      Thanks Marvin, glad it was helpful.

  • @ExplorationAT
    @ExplorationAT 2 ปีที่แล้ว +2

    on point explanation!

  • @danma647
    @danma647 ปีที่แล้ว

    I don't quite get how the adding worked on line 26... also how come it doesn't work if you flip it over as freqCounter[num] = 1 || freqCounter[num] + 1 ?

  • @SagarKumar-db2xy
    @SagarKumar-db2xy 2 ปีที่แล้ว +2

    God or what?

  • @lucasrendo3542
    @lucasrendo3542 ปีที่แล้ว

    Great stuff! One question though, could you even further improve the calculation time by having freqCounter1 store the keys already as num**2. That way you save the calculation time of doing it twice on the loop for freqCounter2. I don't know if that could make a difference with big arrays or not, just wondering