Find second largest number in set Dry Run in PYTHON

Find second largest number in set is an interactive PYTHON dry run visualizer from the Sets programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Find second largest number in set Program Code

# Find second largest in set
numbers = {74, 18, 93, 35, 61, 26, 82, 49}
print("Set:", numbers)

# Method 1: Using sorted
sorted_unique = sorted(numbers)
if len(sorted_unique) >= 2:
    print(f"Second largest: {sorted_unique[-2]}")

# Method 2: Single pass
largest = second = float('-inf')
for num in numbers:
    if num > largest:
        second = largest
        largest = num
    elif num > second and num != largest:
        second = num
print(f"Single pass: {second}")

View the complete Sets programs page.

Program Console Find second largest number in set Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.