CodePark/ problems / Second Largest Value
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Foundations

Second Largest Value

easyabout 12 minutes in an interview

Write secondLargest(nums) that returns the second largest distinct value in nums.

  • Distinct means repeated values count once. In [4, 9, 9, 7] the largest is 9 and the second largest is 7, not 9.
  • If there is no second distinct value, return null. That covers an empty array, a one-item array, and an array where every item is equal.
  • Numbers can be negative. They are all finite, so there is no Infinity or NaN.
  • The array can be in any order. Don't change it.
Input: nums = [4, 9, 9, 7] Output: 79 appears twice but counts once.
Input: nums = [-3, -1, -2] Output: -2
Input: nums = [5, 5, 5] Output: null