Optimized Solutions: Frequency Counters

Optimized Solutions: Frequency Counters

No more nested loops! During my ongoing journey to become a strong problem solver, I’m quickly learning about the many patterns that can help solve algorithms that may appear as tricky. Take this problem for example:

During my ongoing journey to become a strong problem solver, I’m quickly learning about the many patterns that can help solve algorithms that may appear as tricky. Take this problem for example:

- Write a function that takes in two arrays
- Each a array has a set of integers
- Return true if each index of the second array
contains the squared values of the first array
- The frequency of the values must be the same
[1, 2, 4], [1, 4, 16] => true
[1, 3, 8], [1, 9] => false

So just by reading this, it’s clear that we definitely need to iterate through the arrays, implement some conditional statements, and return a boolean. Let’s take a brute force approach to this problem before cleaning it up:

const hasSquared = (array1, array2) => {
  if (array1.length !== array2.length) return false;

  for (let i = 0; i < array1.length; i++) {
      const correctIndex = array2.indexOf(array[i] ** 2);
      if (correctIndex == -1) return false;
      array2.splice(correctIndex, 1);
  }
  return true;
}

For this solution, we first check to see if the frequency/length of the arrays are equal. Next, we use indexOf() *to locate the index of *array1's squared values within the second array. If the index isn’t found then we return false. If we do find an index containing squared values, we splice it from the array and continue looping. We then return *true *once we exit our loop, confirming that *array2 *has the same frequency as *array1 *along with the squared values.

computer-science frequency-counter coding javascript algorithms

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Coding Interviews: Common Algorithms

I previously wrote about some common coding algorithms that you’ll run into on your job search. The article was received fairly well so I thought I’d do a couple more to help in your journey to becoming more familiar with algorithms.

Animated Counter Using JavaScript With HTML CSS | JavaScript Counter Animation

Download The File: https://drive.google.com/file/d/1faoYUjy4jmRntMAXZhoKZ-twtmytAtt5/view?usp=sharing Other PlayList: There is a complete playlist of JavaScr...

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Common Misconceptions About Big-O & Algorithmic Complexity

Are you using Big-O incorrectly? Big-O is a simple and very general concept — the amount of primitive operations needed to complete the algorithm with some parameter.

Coding Interviews: Common Algorithms

Today I am going to be sharing two common coding algorithms as well as their solutions and some links to resources that will help you expand your knowledge of algorithms.