FunctionRacerProblemsRace

Top Three Values

BeginnerSorting and searchingPython, JavaScript, TypeScript, Java, C++

Return the three largest items of nums, from the largest to the smallest. Equal values are separate items, so [5, 5, 2, 1] gives [5, 5, 2].

nums always has at least three items.

Example 1

nums [4,9,1,7,3]
returns [9,7,4]

In order from the top the items are 9, 7, 4, 3, 1. The first three are the answer.

Example 2

nums [5,5,2,1]
returns [5,5,2]

Both 5s count, and then comes 2.

Example 3

nums [3,1,2]
returns [3,2,1]

With exactly three items the answer is the whole list, sorted downwards.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems