Write a method that takes an array of numbers in. Your method should return the third greatest number in the array. You may assume that the array has at least three numbers in it.

Jimmy suggested figuring out how to return the greatest number first.

..then the second greatest:

then the third:

**Provided solution:**

I had this line of thinking going but I didn’t understand how to use nil very well. In this solution first , second, and third are being initialized with ‘nil’ and the current number (incremented by idx) being compared is stored in ‘value’.

compare each number against the rest of the array, once.

if the current value is greater than the first, second, or third, replace it and move other numbers up by 1 respectively.

**Second time solving this by myself:**

My solution is a bit long but it’s pretty intuitive I think. I basically broke the problem into three pieces – greatest, second greatest, and third greatest. First, I wrote a function to find the first greatest number. Then I use the result of that function to write another function to find the second greatest number. Finally, I used the results of both first greatest and second greatest to write a function to find third greatest number.

### Like this:

Like Loading...

*Related*