9. Find Second Largest Element

EasyArrayArray

Given an integer array nums, return the second largest distinct element in the array.

If no second largest distinct element exists, return -1.

Input: An integer array nums of length n.

Output: An integer — the second largest distinct element, or -1 if it doesn't exist.

Examples

Example 1
Input: [10,5,8,12,3]
Output: 10
Explanation: Sorted descending distinct: [12,10,8,5,3]. Second largest = 10.
Example 2
Input: [1,1,2]
Output: 1
Explanation: Distinct: [1,2]. Largest=2, second=1. Return 1.
Example 3
Input: [5,5,5]
Output: -1
Explanation: Only one distinct value (5). No second largest. Return -1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →