The Silver Medal

HardAcc. 85.4%
+40 XP 20

The Second Best

Finding the maximum is easy; finding the runner-up requires tracking two leaders simultaneously.

The Assignment

Your function receives an array scores.

  1. Initialize first and second to a very small number (-Infinity).
  2. Iterate through the array.
  3. If current > first, update second = first and first = current.
  4. Else if current > second and current != first, update second.
  5. Print second. If no such number exists, print -1.

01EXAMPLE 1

Input[10, 5, 10, 8]
Output8

Explanation: 10 is first, 8 is second.

Constraints

  • Single pass (O(n)) only.
  • Do not use .sort().
ArraysLogic
JavaScript
Loading...
2 Hidden

Input Arguments

scores[10,5,8]

Expected Output

8

Click RUN to test your solution