Typorithm · easy

Linear Search

▷ Interactive algorithm labsearchlinear-searchscanlinear-time
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Linear Search

0%

Search Algorithms

Linear Search scans values one by one until it finds the target.

Linear Search is the simplest way to find a value in an array.

It checks each item from left to right and stops as soon as the target is found.

If the target never appears, the function returns -1.

Preparing algorithm lab…
01

Scan from index 0 to the end.

02

Compare each value with target.

03

Return the matching index.

04

Worst-case time is O(n).

function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i;
    }
  }

  return -1;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Function declaration

Receive an array and the value we want to find.

function linearSearch(arr, target)

2. Scan loop

Check each index from left to right until the target is found.

for (let i = 0; i < arr.length; i++)

3. Target check

Compare the current value with the target using strict equality.

arr[i] === target

4. Return index

Return the index where the target was found.

return i

5. Not found

Return -1 after the loop if the target never appears.

return -1

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Linear Search: Visual Walkthrough & Practice · Typorithm | ApplyDjinn