CJCoding With Joseph
15free / daySign inCreate account

Second Largest In One Pass

Write second_largest(values: list[int]) -> int that returns the second largest distinct value in a single pass, without sorting. Track largest and second, both starting at the first element: a value above largest pushes the old largest down into second and takes its place; otherwise a value below largest becomes second when second still equals largest (nothing lower seen yet) or when the value beats second. The list always holds at least two distinct values. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one line of space-separated integers; the tests print the value your function returns.

Expected Output:

7

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & Algorithms
📥 Auto-Input:
3 9 5 9 7\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.