Ctrl K

Third Largest Distinct Value

Easy
You are given an integer array values. List all **distinct** values of the array in descending order, writing equal numbers only once. Return the third number in that list. If there are fewer than three distinct values, return the largest value of the array instead.
Example 1
values=[7, 10, 7, 4, 10, 1] → 4

The distinct values in descending order are 10, 7, 4, 1. The third one is 4; the repeated 10 and 7 count once.

Example 2
values=[5, 5, 2] → 5

There are only two distinct values (5 and 2), so there is no third one and the largest, 5, is returned.

Example 3
values=[-3, 0, -8, 0] → -8

The distinct values are 0, -3, -8. The third largest is -8.

Constraints
  • 1 ≤ values.length ≤ 10⁴
  • −2³¹ ≤ values[i] ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMerge Into the Spare Tail EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyMake It to Every Meeting EasyThe Lost Ticket Easy
loading Python…
Input
values=[7, 10, 7, 4, 10, 1]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 130 tests on the server, hidden ones included.