Watching builds intuition — passing 235 judged problems lands the offer. Get interview-ready with DSA Course.

Start free

Linear Search

Beginner
Search

Linear Search scans each element one by one from left to right until it finds the target value or exhausts the array. Simple and elegant — set a target below to begin.

Search Target

Quick examples:

↑ Set a target value above to begin searching the array

Loading controls...
Loading visualization...
Time: O(n)
Space: O(1)

Legend

Currently Checking
Element being compared to target
Already Scanned
Checked — did not match
Found!
Target value located here
Not Found
All elements checked, no match
Not Yet Checked
Waiting to be searched
— — — target: N
Dashed line marks target height
Reading the chart:
  • • Bar height = element value
  • • Dashed line = target value level
  • • Numbers above bars = values
  • • Numbers below bars = array indices

Inspector

No active frame to inspect

Reference implementation of Linear Search. Select a language tab to view and copy the code.

def linear_search(arr: list[int], target: int) -> int:
    for i, val in enumerate(arr):
        if val == target:
            return i
    return -1

Python — Code Walkthrough

  1. enumerate — index and value together

    for i, val in enumerate(arr)

    enumerate yields (index, value) pairs, so both i and val are available without arr[i]. This is more idiomatic Python than a manual range(len(arr)) loop.

  2. Direct value comparison

    if val == target: return i

    Compares the current value to target. Returning i immediately short-circuits the loop — Python will not check any remaining elements.

  3. Return -1 after the loop

    return -1

    Python has no break-with-value syntax; the -1 return after the for loop body is reached only when the loop completes without finding target.