Find Second Largest Element in an Array

Implement findSecondLargest

Given an array of integers nums, find and return the second largest distinctDistinctA different value from the largest — duplicates of the largest element don't count as a separate "second largest". element in the array. If no such element exists — for example, when every value in the array is the same — return -1.

Example 1:

Input: nums = [12,35,1,10,34,1]

Output: 34

Example 2:

Input: nums = [10,10,10]

Output: -1

Example 3:

Input: nums = [5,1]

Output: 1

+ 10 hidden test cases run on Submit.

Constraints:

  • 2 ≤ nums.length ≤ 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • If every element is equal, there is no second largest — return -1 in that case

nums =

[12, 35, 1, 10, 34, 1]