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 null if there are not at least 2 unique numbers
  • Handle null or empty lists by returning null

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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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

Note

You can test your code by connecting to Salesforce, but to save your progress and earn points, you'll need to create an account. Your solutions and achievements will be tracked automatically once you're logged in.