Write a method that takes a List<Integer> and returns the second largest unique number in the list. If there is no second largest number (the list has fewer than 2 unique numbers), return null.
Requirements
- Find the second largest unique value
- Handle duplicates correctly - they do not count as separate values
- Return
nullif there are not at least 2 unique numbers - Handle
nullor empty lists by returningnull
Examples
findSecondLargest(new List<Integer>{1, 2, 3, 4, 5})
// Returns: 4
// Explanation: Largest is 5, second largest is 4
findSecondLargest(new List<Integer>{10, 10, 10})
// Returns: null
// Explanation: Only one unique number (10)
findSecondLargest(new List<Integer>{5, 5, 3})
// Returns: 3
// Explanation: Unique values are [5, 3], second largest is 3
findSecondLargest(new List<Integer>{1})
// Returns: null
// Explanation: Only one number in list
findSecondLargest(new List<Integer>{})
// Returns: null
// Explanation: Empty list
findSecondLargest(null)
// Returns: null
// Explanation: Null list
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 July 2026
Contest runs July 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.