Easy
Find the Second Largest Distinct Element
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- Arrays and Strings
Description
Given an array of integers, return its second largest distinct value.
Distinct means that duplicate occurrences of the same value count only once. For example, in [8, 8, 5, 3], the largest distinct value is 8 and the second largest distinct value is 5.
If the array contains fewer than two distinct values, return -1.
Implement secondLargestDistinct(nums) to return the second largest distinct element or -1 when it does not exist.
Constraints
1 <= nums.length <= 1000 -1000000 <= nums[i] <= 1000000 Return -1 if fewer than two distinct values exist.
Sample cases
3Sample 1
Input
nums = [3,1,9,2]
Expected output
3
Sample 2
Input
nums = [5,5,5]
Expected output
-1
Skills and languages
Concepts
- Hash Sets
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer