JavaScript: How to compare elements in an array?

Kody Samaroo
5 min readMay 25, 2021

This blog will examine arrays in JavaScript and how to manipulate the elements inside and predict outcomes. In order to illustrate this we will look at common entry level interview questions and walk through them to better understand arrays and how to approach problems in a similar vein.

I. Palindrome

A common entry level interview question is to compare the elements within an array to meet a certain expectation. Typically this question is phrased as “Finding the palindromes” or reversing strings but the general idea involves manipulating data types within the array. An array is just list of strings, numbers or any other data types. We can iterate through every element in an array and compare it to meet an expectation, then return an output were the results are true. If you stumbled here looking for a StackOverflow post on how to find a palindrome here is the basis of that code.

function palindrome(string) {
return string == string.split("").reverse().join("")
}
// outputs a boolean due to the "==" operator

The function takes a string parameter which will be used as the criteria in the comparison. Since a palindrome is the same front to back, a string like “level” will look the same both ways. So now we can create some logic to manipulate the string and test our output against the original. split(“”) will take each character of a string and create an array where each element will be a letter from that string.

reverse() is an action that does exactly what the name suggests, it will morph this array of characters to its inverse. Then we use the join(“”) action to join every character in the array back together. Due note that with this method, because we are only dealing with one variable it will use less memory (see big O notation) but we will lose access to this reversed string outside the scope of this function.

Now what if we are given an array of strings and want to test every string? or if we want to find the biggest number in array of numbers?

We will need to iterate over each element and then run some logic and test our output. The most basic step for this is to create interval and set it equal to 0, this will be the first index in the array. Then we setup a while loop where we can put our logic.

let i = 0while ( i < array.length ) {
// code logic
}

And here is an example of what that code block may look like with everything together.

The “i” value is the interval and will move through the array indices. The comparison logic is much the same with the addition of toString() which will ensure all the data types are the same, a string. I included a results array to return a clean output, due note that this returns a new variable that won’t be accessible outside of this function scope.

II. Sort

Another often asked question is how to sort elements in an array. The way to achieve this is through the sort() method for arrays. This works fine for strings but with numbers the first digit’s place from 1–10 is used to sort numbers.

As shown in the image above an array with [1, 10, 100, 25] is the output.

This could be useful but for most cases your employer wouldn’t care to see a collection of numbers arranged this way. Ascending order can be achieved by using a function sort and compare number with another number in the array. One number subtracted by another number will place the larger of the two numbers at the end and order an array ascending.

Swapping the a and b will sort the biggest number on the left achieving a sorted list in descending order. The array on the output of this function By using a function with two parameters, a and b to hold the value two elements we can compare them by subtracting them and whichever is larger will be placed on the right. The function will continue to run and compare all the elements until there is only one number left, the largest one and place it at the end of the array. Note sort does not return a new array but just modifies the array within the scope.

Conclusion

There are many ways to manipulate the data in the array it all depends on the data type. The main take-away is to pay attention to your data through all stages of the function scope. What is my elements data type? What logic/comparison is being performed? What is my desired output? All are valid questions that should steer the construction of your code. Walk through it step by step, visualize the data, predict the output and consistently ask these questions while programming.

Related Links

--

--