Free beta: 60 days of full access, no card needed.120 seats leftSign up free

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy

All patterns

Linear search

O(n)

Walk the collection from one end and stop at the first item that matches. No order, no structure and no setup are needed.

Updated Aug 24, 2026

How does Linear search work?

Start at the first element of the collection. Ask whether it is the one you want.

If it matches, stop and return its index. There is no reason to look further.

If it does not, move to the next element. Ask the same question again.

When the end arrives with no match, report failure. Minus one or null is the usual answer.

Nothing at all is assumed about the data. It may be sorted, shuffled or still arriving.

The worst case reads every element once. The best case reads exactly one.

  1. i = 0, value 5Looking for 2 in [5, 8, 2, 9]. The first element misses.
  2. i = 1, value 8Still no match. Move on to the next index.
  3. i = 2, value 2This element equals the target.
  4. return 2The index goes back at once. The 9 is never read.
  5. target 7 gives -1A missing value costs a whole pass. That is the worst case.

The Linear search code template

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

A worked example of Linear search

First character that never repeats

You get a string. Return the index of the first character that appears exactly once.

If every character repeats, return minus one.

Count every character in one pass. A map keyed by the character is enough.

Then scan the string again from the left. Return the first index whose count is one.

function firstUniqChar(s) {
    const count = new Map();

    for (const c of s) {
        count.set(c, (count.get(c) ?? 0) + 1);
    }

    // the second pass is a plain scan: the first hit wins
    for (let i = 0; i < s.length; i++) {
        if (count.get(s[i]) === 1) return i;
    }

    return -1;
}

When should you use Linear search?

These phrases in a problem statement point here:

  • unsorted array
  • check every element
  • no order guarantee
  • find first/any match
  • small n or one-time scan

What is Linear search confused with?

Common mistakes with Linear search

  • Scanning inside another loop

    A scan per element turns O(n) into O(n squared). Build a map once instead.

  • Returning the value instead of the index

    Most of these tasks want the position. A value cannot say where it was found.

  • Forgetting the not-found case

    A loop that ends with no match still has to return something. Pick minus one and stay with it.

  • Scanning data that was already sorted

    Sorted input makes binary search far cheaper. Check the input before writing the loop.

Which interview problems use Linear search?

  • Linear search: The plain form: stop at the first match.
  • First unique character in a string: Count once, then scan for a count of one.
  • Find the maximum: Keep the best seen and compare each element against it.
  • Contains duplicate on tiny input: A nested scan is fine when n is very small.
  • Missing number: Scan and compare each index against the value expected there.
  • Find all indices of a value: The same scan, but it does not stop at the first hit.
  • Majority element: One pass with a counter gives the Boyer-Moore vote.

What is the time and space complexity of Linear search?

O(n)

n up to roughly 1e7 gives O(n) time and O(1) space. Bigger, or repeated, wants a real structure.

See where this fits in the 150-step track