FastPrepSecond Largest Distinct Element

Second Largest Distinct Element

Celebal Technologies logoCelebal Technologies● EasyNEW GRADPHONE SCREEN

Problem statement

Given an integer array nums, return its second largest distinct value. Repeated copies of a value count only once.

The array has at least two distinct values. Do not use built-in sorting functions. Use one traversal and constant extra space.

Function

secondLargestDistinct(nums: int[]) → int

Examples

Example 1

nums = [10,5,20,8,20,15,7]return = 15

The distinct values are 5, 7, 8, 10, 15, and 20, so 15 is second largest.

Example 2

nums = [-4,-1,-1,-7]return = -4

The distinct values are -7, -4, and -1; the result is -4.

Constraints

  • 2 <= nums.length <= 10^5.
  • -10^9 <= nums[i] <= 10^9.
  • The array contains at least two distinct values.
See Celebal Technologies hiring insights
Saved
public int secondLargestDistinct(int[] nums) {
    // Write your code here.
}
nums[10,5,20,8,20,15,7]
expected15
Checking account…