Find the median value of a list of integers. The median is the middle value when the list is sorted in ascending order. For even-length lists, the median is the average of the two middle values. Return null for null or empty lists.
Examples:
Input: numbers = [3, 1, 2]
Output: 2.0
Explanation: Sorted list is [1, 2, 3], the middle value is 2
Input: numbers = [4, 1, 3, 2]
Output: 2.5
Explanation: Sorted list is [1, 2, 3, 4], the two middle values are 2 and 3, and their average is 2.5
Input: numbers = [5]
Output: 5.0
Explanation: A single element is the median
Input: numbers = []
Output: null
Input: numbers = null
Output: null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.