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!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow 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 May 2026
Contest runs May 1 - 31. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.