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

  Рет қаралды 2,224

DoableDanny

DoableDanny

3 жыл бұрын

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 2 жыл бұрын
A friend sent me your article, I just came here to say good work.
@doabledanny
@doabledanny 2 жыл бұрын
Thanks Marvin, glad it was helpful.
@ExplorationAT
@ExplorationAT 2 жыл бұрын
on point explanation!
@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
@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 жыл бұрын
God or what?
@traderzgaming
@traderzgaming Жыл бұрын
jod or wot😁
Array Methods in JavaScript | 17 Useful Methods
42:39
DoableDanny
Рет қаралды 48 М.
МАМА И STANDOFF 2 😳 !FAKE GUN! #shorts
00:34
INNA SERG
Рет қаралды 3,6 МЛН
Haha😂 Power💪 #trending #funny #viral #shorts
00:18
Reaction Station TV
Рет қаралды 14 МЛН
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 68 МЛН
Problem Solving Patterns in Apex: The Frequency Counter
22:44
Software Engineering and Salesforce
Рет қаралды 1,1 М.
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,1 МЛН
9.15: 2D Arrays in JavaScript - p5.js Tutorial
12:38
The Coding Train
Рет қаралды 115 М.
How to Implement a Hash Table in JavaScript
25:39
Ben Awad
Рет қаралды 102 М.
🧠Mastering Algorithms: Frequency Counter Pattern
12:20
Code the Things
Рет қаралды 2,5 М.
Local Storage & Session Storage [ with Code Examples ]
14:52
Akshay Saini
Рет қаралды 216 М.
Finding Matches in Arrays
8:30
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 23 М.
Must-Know JavaScript Algorithms for Beginners || Big O Notation
9:21
МАМА И STANDOFF 2 😳 !FAKE GUN! #shorts
00:34
INNA SERG
Рет қаралды 3,6 МЛН