Algorithm Time: Are There Duplicates?

Daniela Sandoval
Nov 11 · 6 min read

Trying to figure out whether or not you’ll be parenting triplets, quadruplets, or just duplicates in your function.

I’m still waddling around on the 2-feet side of the algorithm pool, but I’ve become familiar with different types of approaches to solving a problem. Yes the idea of different approaches, especially in programming, isn’t new to me, but giving a name and face to common patterns helps a lot when approaching a tantalizing new problem. It’s nice to have a starting line and a general direction to go in by using problem solving patterns. This week I’ll be focusing on two in particular that Colt Steele refers to as frequency counters and multiple pointer patterns. If algorithms are not your jam just yet, try coming up with several different solutions for ones that you are comfortable with. This post will be like a choose your own method style and remember, solutions are not limited to the ones you see! So let’s dive in.

The “hard way” being attempting to solve algos in different run times after finally getting that one solution that gives you the correct outputs.

The Problem 🤷🏻‍♂️

Here’s what we have to work with.

Implement a function called, areThereDuplicates which accepts a variable number of arguments, and checks whether there are any duplicates among the arguments passed in.areThereDuplicates(1, 2, 3) //false
areThereDuplicates(1, 2, 2) //true
areThereDuplicates("c", "a", "t", "s", "s") //true

Luckily for us, this prompt is pretty straight forward. We are checking to see if there are any repeats or values that are the same. Usually we’d see a structure like arrays as our input, but here we see “a variable number of arguments”. When creating our solution, we’ll deal with how to access and handle arguments. What we do know is that we are going to receive some form of a collection of values. The second point to notice is that our output will be a boolean value of either true or false. As for the “how in the world are we going to do this” part of our logic, checking to see if two values are the same sounds like comparisons so we’ll be throwing a little bit of that into our concoction.

Breaking It Down 🎧

Now for this particular post, you could read it in a choose your own adventure style where I will give three different kinds of approaches. The first two deal with a problem solving pattern while the last one is more of a bonus round.

The frequency counter pattern at keeping track of the frequency of data through an object. After we’ve constructed this object, we can use it to determine our output! It’s kinda like a mini-inventory of what we have.

function areThereDuplicates() {
1. Create our counter (object).
2. Loop through our arguments.
a. If that argument is in our object, add 1 to it.
b. If it's not in our object, create it in our object.
3. Check our newly made argument by looping through it to see if there are duplicates.
a. If a keys value has more than 1 count, then there duplicates
4. If we finish the loop without returning anything, then there are no duplicates
}

The multiple pointer pattern is useful for avoiding looping within another loop. By using a loop, we use indices to traverse through a structure, but it’s usually restricted to one point of reference: i. If you use a multiple pointer pattern, we can maximize our loop by have two or more variables to use for indices. Two i‘s are better than one! (🥁)

function areThereDuplicates() {
(Can we turn our arguments into a sorted array???)
1. Create our first point of reference starting at 1.
2. Loop through our sorted array to create the second point of reference starting at the beginning of the array (0).
a. If an element is equal to the next element, return true.
b. If not, then continue adding to our points.
3. If by the end of our looping we haven't found a duplicate, then looks like there aren't any so return false.
}

I’ve realized that those one liner solutions I always stare at amazement often come after analyzing a problem for a good chunk of time. Sometimes you can arrive at a solution with just pure logic and it’ll have you face palming yourself.

function areThereDuplicates() {
1. What if we take our original arguments collection and compare it to a copy that has weeded out the duplicates...?
a. what does it mean if they aren't the same?
}

Let’s Code! 👩🏻‍💻

Unlike my previous algorithm ‘let’s code’ section where I went step by step, this section will just highlight what makes each pattern unique. It won’t be a detailed guide, but I won’t leave you in the dark either. An important thing to note is that we can access any arguments passed into our function by calling the arguments object

What makes this pattern work is the ability to create the object. Without, we can’t see what we’ve counted.

let frequencyCounter = {};
2. Loop through our arguments.
for(let val in arguments) {
a. If that argument is in our object, add 1 to it.
b. If it's not in our object, create it in our object.
frequencyCounter[arguments[val]] = (frequencyCounter[arguments[val]] || 0) + 1
}

For our pointers, we need to make sure we properly use them while iterating. Also don’t forget to increase any other pointers that aren’t controlled by your loop. Although arguments looks like an array, it more resembles an object so I took the liberty to turn our input into a sorted array with the help of Array.from().

let j = 1
2. Loop through our sorted array to create the second point of reference starting at the beginning of the array (0).
for(let val in sortedArr) {
a. If an element is equal to the next element, return true.
if(sortedArr[val] === sortedArr[j]) {
return true
} else {
b. If not, then continue adding to our points.
j++
}
}

By turning our arguments into an array, we can rid it of any duplicate values by using Set() to determine if the newly made array is the same as the argument. We do so by looking at how big the collections are. If the two are not same, that means we had duplicates somewhere that are no longer there.

1. What if we take our original arguments collection and compare it to a copy that has weeded out the duplicates...?
return new Set(arguments).size !== arguments.length

Our Duplicates Solutions 🌟

Final Thoughts… 🍵

Photo by Jørgen Håland on Unsplash

I know creating two or even three different solutions for a problem seems like overkill, but it really does help you understand exactly what’s going on in your function. You get to become familiar with every nook and cranny of your solution and your understanding of what it accomplishes. By creating them, you also start to become accustom to noticing that some solutions would be more optimal if your input had several hundred values (cough big o cough). Thank you for reading my duplicate solutions for a duplicates algorithm!

JavaScript in Plain English

Learn the web's most important programming language.

Daniela Sandoval

Written by

Software Developer | Flatiron Alumni | Proud cat mom! 🐈 💻 ✨

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade