Finding the Top Three Largest Numbers Algorithm

Hope everyone has had a happy holiday so far during this festive season! This week I’ve focused on longer algorithm challenge questions which focus on creating then calling helper methods to practice better habits at organizing my code while applying more logic on how to manipulate arrays. One of the algorithms I’ve solved for this week is the question that asks for the top three greatest numbers without sorting the array from least to greatest.

The trick with this question is to think in steps on how to solve it and then then knowing how to designate ultimate goals for each helper method written. Let me also add that again I will be solving this in JavaScript. My explanation for this one is on the longer side, but I have tried to simplify it as much as possible. Let’s dive right in.

The Problem

As stated earlier, our goal is to return an array with the three largest numbers within the input array, which from the start is supposed to have at least three integers within it. These integers from the input array may not be sorted, but the question asks us not to sort them in our function. If there are duplicate integers from our input array, it should return them if necessary.

The other thing to mention is that the numbers returned in the final answer will be in order from least to greatest. For example if the top three numbers are 5, 6, 7 then the function should return [5, 6, 7] in that order.

Solution

To start off, we need to write our main function which I’ve called findThreeLargestNumbers. The argument that will go in will be our input array as stated in the problem.

Then we need to initialize our array to have three blank spaces for our largest numbers by setting each of those spaces to null. We are setting each space equal to null so we can compare null to actual numbers by using the greater or less than symbols later on.

We need a way to look through each numbers in our array so we will start a for of loop below, and each time a number is looked at for each round of the loop we will call updateLargest which is the name of our helper method which will do some of the main number comparison logic. The arguments that get passed in will be the threeNumbers array and the number we are on so that the helper method has access to these values. At the end of it all, we will return our three numbers because that is what our question is asking for.

To write our helper method we will open it up by coding the same name and the same arguments seen before. Next we will use an if condition to check if the last number in the threeNumbers array, since arrays start their count from 0, is null with no real number inside or if the current number is bigger than the third number.

If this condition is true, then the shiftToUpdate method is called. In this second helper method, its main goal will be to update our top three number array by shifting the numbers in the appropriate place. The thing to recognize in this next part down below is that we will pass in the top three array of course, the number we are on, and the number 2 to represent the last index because that is the last index we are on that we would like to update.

At this point, we will extend this updateLargest method to include two else if conditions that will essentially do the same thing except for if the if condition to it previous is not true then it will change the number places one space to the left as shown.

This concludes this helper method. Now we need a way to write a check to see if our index that we are checking is equal to our index implemented, then we need to change that number’s index accordingly in our final three number array. The second helper method’s name is shiftToUpdate which will take in the threeNumbers array, the number we are on, and an index to compare.

We will use a for loop to look through our indexes and if the index we are on is equal to the index of the number we are checking then we set that value to be assigned to the number variable. Otherwise we will be shifting the index based on the context of the comments below.

That concludes the solution for this algorithm challenge. Hope your new year goes well and I hoped this helped you to understand how this algorithm works better.

Irene Scott has a passion for coding/learning new things. When she is not coding, she enjoys the Florida sunshine, going to the dog beach, and orange picking.