Given a list of integers, return the third distinct maximum number. If the third distinct maximum does not exist, return the overall maximum instead. Return null for null or empty input.
Examples:
Input: nums = [3, 2, 1]
Output: 1
Explanation: The third distinct maximum is 1.
Input: nums = [1, 2]
Output: 2
Explanation: The third distinct maximum does not exist, so return the maximum which is 2.
Input: nums = [2, 2, 3, 1]
Output: 1
Explanation: The distinct values are {1, 2, 3}. The third distinct maximum is 1.
Input: nums = [5]
Output: 5
Explanation: Only one distinct value. No third maximum, so return the maximum which is 5.
Input: nums = []
Output: null
Explanation: Empty list returns 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 September 2026 (Sep 1–15)Sponsored by FLXBL
Contest runs September 1 - 15. Complete challenges to climb the leaderboard!
1st & 2nd: Salesforce certification exam voucher — see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.