Free beta: 30 days of full access, no card needed.Sign up free

We use necessary cookies to run the site (sign-in and language). The contact and bug-report forms also use Google reCAPTCHA for spam protection, loaded only if you accept. Privacy policy

Field manual

Linear search

O(n)

Walk the array once, checking every element in order, when there's no structure to exploit yet.

Signals

unsorted arraycheck every elementno order guaranteefind first/any matchsmall n or one-time scan

Template

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

Looks similar, isn't

  • Binary search: Binary search needs sorted data and n large enough that halving matters. On unsorted input or tiny n, sorting first (or at all) costs more than it saves, so a plain scan wins.

no order guarantee, n up to ~1e4..1e5 with a single scan -> O(n) time, O(1) space. If n is large AND the data is sorted, that's the cue to reach for binary search instead.

Learn this pattern