easy +10 pts

Find Second Largest

Return the second largest unique element from a list of numbers.

Write a function `second_largest(nums)` that takes a list of integers (or floats) `nums` and returns the **second largest distinct value**. If the list has fewer than two distinct values, return `None`. - Only unique values are considered (e.g., for `[5, 5, 3]`, the distinct values are 5 and 3, so the second largest is 3). - The input list may be empty. - The input may contain negative numbers. - Do not modify the original list. Your task: implement the function exactly as specified.

Constraints

- `0 <= len(nums) <= 10^5` - Values are integers or floats within typical Python range. - Time complexity should be O(n log n) or better (e.g., O(n) is preferred).

Example

['>>> second_largest([1, 2, 3])', '2', '>>> second_largest([3, 3, 3])', 'None', '>>> second_largest([5, 5, 4, 4, 3, 2])', '4', '>>> second_largest([])', 'None']
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `set()` to remove duplicates.
If the set has fewer than 2 elements, return None.
Sort the set or use two passes to find max and second max.
For O(n) time, track the two largest values in one pass.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.