Find first occurrence of element Dry Run in PYTHON

Find first occurrence of element is an interactive PYTHON dry run visualizer from the Recursion 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 first occurrence of element Program Code

def first_occurrence(arr, target, idx=0):
    if idx == len(arr):
        return -1
    if arr[idx] == target:
        return idx
    return first_occurrence(arr, target, idx+1)

arr = [5, 3, 8, 3, 9, 8, 2]
target = 8
pos = first_occurrence(arr, target)
print(f"First {target} at index {pos}")

View the complete Recursion programs page.

Program Console Find first occurrence of element Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.