FunctionRacerProblemsRace

Median of the List

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

Return the median of nums: put the numbers in order, and if there is an odd number of them take the middle one; if there is an even number, take the average of the two middle ones.

The answer can be a whole number or a half, like 2.0 or 2.5. Do not change nums.

Example 1

nums [3,1,2]
returns 2.0

In order: 1, 2, 3. The middle one is 2.

Example 2

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

In order: 1, 2, 3, 4. The two middle ones are 2 and 3, whose average is 2.5.

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