Find last occurrence of element Dry Run in PYTHON

Find last 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 last occurrence of element Program Code

def last_occurrence(arr, target, idx=0):
    if idx == len(arr):
        return -1
    result = last_occurrence(arr, target, idx+1)
    if result != -1:
        return result
    return idx if arr[idx] == target else -1

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

View the complete Recursion programs page.

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