Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSASearchLinear SearchAlgorithms

Linear Search — Algorithm Visualized

Learn linear search — sequential scan O(n), works on unsorted arrays, step-by-step index highlight visualizer, code and complexity analysis.

Jul 4, 20263 min read

Introduction

Linear search checks each element from left to right until the target is found or the array ends — O(n) time, O(1) space, works on unsorted data.

Quick index

#Section
1How it works
2Interactive visualizer
3Full implementation
4Complexity analysis
5Real-life examples

1. How it works

plaintext
for i = 0..n-1: if arr[i] === target → return i
return -1

Back to index


2. Interactive visualizer

Linear Search — Step Visualizer

Scan each element left to right until target is found or array ends.

Target: 7 · Start

4
2
7
1
9
3

Linear search — scan left to right

const target = 7
for (let i = 0; i < arr.length; i++)
  if (arr[i] === target) return i

Check each element until found or array exhausted.

Time

O(n)

Space

O(1)

Stable

—

Back to index


3. Full implementation

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

Function calls with output

js
// ── Demo calls — output shown on the right ──
const arr = [4, 2, 7, 1, 9, 3]
 
linearSearch(arr, 7) // → 2
linearSearch(arr, 1) // → 3
linearSearch(arr, 99) // → -1

Back to index


4. Complexity analysis

CaseTime
BestO(1) — target at index 0
WorstO(n) — target absent or last
SpaceO(1)

Note

Use linear search when data is unsorted or small. For sorted arrays, binary search is O(log n).

Back to index


5. Real-life examples

ScenarioWhy linear search
Finding a contact in unsorted phone listNo order — must scan each entry
Barcode scan mismatchCheck if SKU exists in unsorted shipment list
Ctrl+F in unsorted documentScan text character by character
Checking username availability (small list)Reserved names array — n < 100, linear is fine
Finding item in messy drawerNo index — check each object until found

Warehouse return lookup

Scan unsorted daily return labels — must check each entry until found.

R-88210[0]
R-90121[1]
R-77432[2]
R-33013[3]

Today's returns — no sort order, FIFO scan required.

Real-world implementation

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

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const returns = ["R-8821","R-9012","R-7743","R-3301"]

Side output shows return value after each call — step through to trace execution.

Note

Once a list grows or gets queried repeatedly, sort + binary search or a HashMap replaces linear search.

Back to index


Summary

Linear search is the baseline — simple, no preprocessing, optimal when n is small or data is unsorted.

Next reads: Binary Search · DSA Arrays

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime