easy +10 pts

Second Largest Unique Value

Find the second largest distinct number in a list of integers.

Write a function `second_largest_unique(nums)` that takes a list of integers `nums` and returns the second largest **distinct** value. If there are fewer than two distinct values, return `None`. For example, `[3, 1, 4, 4, 2]` has distinct values `{1, 2, 3, 4}`, so the second largest is `3`. If the list is `[5, 5, 5]`, there is only one distinct value, so return `None`. Implement the function with the exact signature: `def second_largest_unique(nums):`

Constraints

- `0 <= len(nums) <= 10^5` - Each element is an integer in the range `-10^9 <= nums[i] <= 10^9` - The function should run in `O(n)` time and `O(n)` space.

Example

```python
>>> second_largest_unique([3, 1, 4, 4, 2])
3
>>> second_largest_unique([5, 5, 5])
None
>>> second_largest_unique([10])
None
>>> second_largest_unique([2, 2, 1])
1
```
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a set to remove duplicates, then find the largest and second largest.
You can sort the unique values and pick the second from the end.
Remember to handle the case where there are fewer than two unique values.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.