# 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.

`function findThreeLargestNumbers(array) {`

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.

`let threeNumbers = [null, null, null]`

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.

`for (const number of array){`

//for each number of the input array, call the helper method

//update largest and pass in the three number array and the number are on

updateLargest(threeNumbers, number)

}

return threeNumbers

//eventually return the three numbers

}

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.

`function updateLargest(threeNumbers, number){`

if(threeNumbers[2] === null || number > threeNumbers[2]){

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.

`shiftToUpdate(threeNumbers, number, 2)`

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.

`} else if (threeNumbers[1] === null || number > threeNumbers[1]){`

//otherwise if our second largest number is null or if the number we are on

//is bigger than the second number in our numbers array

//then call our helper method and pass in the three numbers array,

//the number we are on and the second index

shiftToUpdate(threeNumbers, number, 1)

} else if (threeNumbers[0] === null || number > threeNumbers[0]){

//otherwise if our smallest number in our top three numbers

//is null or if the number we are on

//is bigger than the first number in our numbers array

//then call our helper method and pass in the three numbers array,

//the number we are on and the first index

shiftToUpdate(threeNumbers, number, 0)

}

}

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.

`function shiftToUpdate(array, number, index){`

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.

`for(let i=0; i <=index; i++){`

if (i === index){

//update the last index to be the last number in our array

array[i] = number

} else {

//otherwise set a number we are on assigned to the number we are on plus on to increase its place in the final array

//So in the array the first number in the three numbers array will be equal to the second index

//the number in the second place will now be equal to the 3rd index, and the last number will

//become the value of whatever number variable is

array[i] = array[i +1]

}

}

}

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.