Frequency Counter vs Multiple Pointers
By: Christy Tropila
Tags: Interview, Algorithms, JavaScript, Data Structures, Computer Science

Learning Data Structures and Algorithms is an essential part of being a Software Engineer. In the simplest terms, data structures are structures in which we organize data and algorithms are the set of instructions to perform a task.

This past week I decided it was time to dive into this topic and prepare myself for any future interviews. I bought the JavaScript Algorithms and Data Structure Masterclass instructed by Colt Steele on Udemy to help me accomplish my goals. You can find more information on this course here.

This is not my first attempt at trying to learn data structures and algorithms. I was always too intimidated by the Big O mathematics and found myself giving up within the first 5 minutes. Steele does a great job explaining concepts for beginners. He lays out a solid problem-solving approach and several problem-solving patterns that I feel will help solve any interview question. I want to go over 2 patterns that I find super useful and have been using this week.

Frequency Counter && Multiple Pointers

The idea of a frequency counter is to use an object to collect values/frequencies of values. This can be used to avoid the need for nested loops. (O(n²)).

The multiple pointers pattern creates “pointers” that correspond to an index or position and moves towards the beginning, end, or middle based on a condition. With multiple pointers, you want to make sure that whatever data set you are working with, is sorted.

I want to share a simple example of how we could use either one of these patterns to solve it.

Interview Question

Implement a function called checkForDuplicates which accepts a variable number of arguments(any number of arguments) and checks whether there are any duplicates among the arguments passed in.

Multiple Pointers Approach

The first thing I like to do is restate the problem in my own words. For this example, I might restate it like so: Given an argument, check to see if there are any 2 that are the same. If this is true, return true, otherwise, return false. Next, I like to come up with some concrete examples.

checkForDuplicates([1,4,7,7]) => true

checkForDuplicates(['a', 'b', 'c'])=>false

checkForDuplicates([2,2,4])=>true

3. After visualizing some possible outcomes, I start to pseudo-code.

//sort the given arguements . I prefer ascending orders

//create a starting point. Lets start at the beginning index of the data

//create a second pointer. For this example I will create it at index 1

//create a while loop that will run while the second pointer is less than the length of the arguement

//compare the two values that the pointers are currently pointing at. If these two values are the same, return true

//increment the 2 pointers until the condition is met

//if no pairs are equal, return false

4. After writing out my thought process, I start to code and solve the problem

function checkForDuplicates(...args){ //sort the given arguements . I prefer ascending order

args.sort((a,b)=> a > b); //create a starting point. Lets start at the beginning index of the data

//create a second pointer. For this example I will create it at index 1

let start=0;

let next=1; //create a while loop that will run while the second pointer is less than the length of the arguement

while(next < args.length){ //compare the two values that the pointers are currently pointing at. If these two values are the same, return true

if(args[start]===args[next]){

return true;

} //increment the 2 pointers until the condition is met

start++

next++

} //if no pairs are equal, return false

return false;

}

//below is the solution without the pseduo comments function checkForDuplicates(...args) {

args.sort((a,b) => a > b);

let start = 0;

let next = 1;

while(next < args.length){

if(args[start] === args[next]){

return true

}

start++

next++

}

return false

}

Frequency Counter Approach

Now I will show you how you can solve this same problem with a different approach. Similar to how I approached the problem with multiple pointers, I reworded the problem in my own words and then created some concrete examples. Since I am using the same problem, you can review my thought process and the steps above to see how I would first approach this. Let's jump right into the pseudo-code for this approach-

//create an empty object that will be used as a counter

//loop through the set of data. If the data is not yet added to our object, add it and set it equal to a count of 1. If the data is found in the object, increment the count by 1.

//loop through our created object to see if there are any values that are greater than 1.

//if any keys have a value greater than 1, return true. If no duplicates are found, return false

Next lets code this solution -