Find the second distinct largest number
Return the second distinct largest number, or null when fewer than two distinct numbers exist. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.
Input
One JSON value: number[]. See the sample for its exact shape.
Output
One compact JSON value of type number | null. Strings include JSON quotes; arrays preserve the specified order.
Constraints
Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range.
Example
Input
[4,1,9,9,7]
Output
7
Example 2 input
[0,-1]
Output
-1
Checks a boundary or contrasting case beyond the public examples.
Public test cases (2)
Input
[4,1,9,9,7]
Expected output
7
Input
[]
Expected output
null
Hints (2)
- Start with the required output order and the empty-input behavior.
- Track two distinct maxima
Target complexity: time O(n) · space O(1)
