All challenges
Easy 0 solved 0% acceptance

Second Largest Value

Find the second largest distinct value.

Problem

Return the second largest distinct value in the list. Repeated values count once.

Implement second_largest(numbers).

Input

numbers: A list of non-negative integers.

Output

Return the second largest distinct value, or -1 if it doesn't exist.

Constraints

0 ≤ length ≤ 100,000; 0 ≤ each value ≤ 10⁹

Examples

Example 1
Input:
numbers = [4, 9, 7, 9]

Expected output:
expected = 7
Input:
std::vector<long long> numbers = {4, 9, 7, 9};

Expected output:
long long expected = 7;
Input:
long[] numbers = new long[] {4, 9, 7, 9};

Expected output:
long expected = 7;

Explanation

Distinct values are 9, 7, 4; the second largest is 7.

Example 2
Input:
numbers = [5, 5]

Expected output:
expected = -1
Input:
std::vector<long long> numbers = {5, 5};

Expected output:
long long expected = -1;
Input:
long[] numbers = new long[] {5, 5};

Expected output:
long expected = -1;

Explanation

Only one distinct value exists, so the answer is -1.

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.