It does not exist. The only distinct value is 5. Your code should recognize this and return a sentinel value like -1 or throw an exception.
Handling Arrays with Duplicate Elements
What is the second largest element of [5, 5, 5]?
The answer
More FAQs in Handling Arrays with Duplicate Elements
If not explicitly ignored, a duplicate of the maximum value will overwrite the second largest variable, resulting in both variables holding the maximum value.
Add a logical AND condition (current != max) when evaluating whether to update the second largest variable.
If the current value equals the max, the first condition (current > max) fails. It falls to the second condition, which also fails because of the current != max check, safely ignoring the duplicate.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.