CodeX
Published in

CodeX

Seek and Destroy — The JavaScript Algorithm

Picture from Loudwire

During our algorithm practice, some exercises are boring, others will be fun, and others you can’t help but blog about them. That’s the case with this fun algorithm I came across a few days ago. Seek and Destroy, the algorithm that made me hear James Hetfield sing out load Searching, Seek and destroy with his recognizable voice. Yes, I’m talking about Metallica and an algorithm that will search the values on an initial array that will be the same as the arguments followed after that array and destroy them.

For example, seekAndDestroy([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].

So let’s blast our headsets with Seek and Destroy and check out some solutions.

Nested Loops

For the first solution, we’re creating an array using Object.values(arguments).slice(1) and storing it in the variable valuesToRemove to compare against arr. We start a basic for loop to iterate through arr. Then, nest another for loop inside the first, changing the integer variable j and arr to valuesToRemove. This second loop will iterate through valuesToRemove. Within the second loop create an if statement, checking strictly === that the current value of arr[i] is equal to valuesToRemove[j]. If the value at the current index is equal in both arrays, we’ll delete it from arr. Outside of the nested loops: return the modified array, filtering out any nulls created by the delete operator.

You might think this code is too complicated, and that’s correct. When we add nested iterations through the input will increase the complexity. According to the Big O Notation, this is an O(n2) function, and it represents a function whose complexity is directly proportional to the square of the input size. Thus, increasing the complexity. In other words, the response will be slower, it will take more memory, and that’s worst than St. Anger’s snare sound. Pretty bad.

Lars looks like he’s bragging about getting up on his feet and drumming abilities. Picture from Louder Sound.

Array Iteration Methods

Our second solution would be the most common JavaScript solution, the Enter Sandman of this exercise. It is iterating the new array with the .filter and .includes methods. As we can see, this is a way cleaner code. Iteration methods such as .filter have a time complexity of O(n), which increases linearly in direct proportion to the number of inputs. At first, the iteration methods can look confusing. Once we understand how to use them, these methods, implementing them as needed, will be as natural as singing along to Enter Sandman in karaoke night.

Retsuko is overreacting to Enter Sandman. Picture from BBC.

Rest Parameters

The third and final example is my favorite of it all. This one is Cliff Burton (RIP) playing bass solo epic level. In this solution, we’re using the rest parameters syntax. We allow a function to accept an indefinite number of arguments as an array using three dots . Then, we return the filtered array with the .includes methods. This solution is some beautiful ES6 code.

Just listen to that electric bass. He was one of the best bass players ever.

So there we have it, a fun algorithm that we can name after a Metallica song. I hope you learned something new with this read and enjoyed it as much as I enjoyed writing it. So keep studying those algorithms and have fun finding something about your favorite music or pop culture to reference it.

Happy coding!

Summary:

  1. Introduction to the “Seek and Destroy” algorithm.
  2. Nested Loops
  3. Array Iteration Methods
  4. Rest Parameters Syntax

References:

  1. Learning Big O Notation With O(n) Complexity by Newton, Dan.
  2. Time complexity Big 0 for Javascript Array methods and examples by Castillo, Luis.
  3. MDN Web Docs, Rest Parameters

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store