Free beta: 60 days of full access, no card needed.84 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 Oct 17, 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.

Sources: Wikipedia: Linear search

  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.
Find 8 by checking every element in turn
linearSearch([7, 3, 9, 4, 8, 2], 8)
arr
7
3
9
4
8
2
target = 8i = -return = ?

Find the index of 8. The array is not sorted, so the only way is to check the numbers one by one from the start.

Speed race
Linear and binary search over the same 40 sorted numbers

Bars: the sorted numbers. Both lanes look for one near the end.

One operation is one comparison with the target. Every frame, each lane does the same number of operations, so the slow one falls behind.

Linear search O(n)
0 operations
Binary search O(log n)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 1,000
Linear search900 · under 1 ms
Binary search10 · under 1 ms
n = 100,000
Linear search90K · under 1 ms
Binary search16.6 · under 1 ms
n = 10,000,000
Linear search9M · 90 ms
Binary search23.3 · under 1 ms

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?

Use Linear search when the problem statement says “unsorted array”, “check every element” or “no order guarantee”. A solution built on it usually runs in O(n).

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

The signal words of every pattern sit in one table, the algorithm patterns cheat sheet.

What is Linear search confused with?

Linear search is most often confused with Binary search (array), Hash set / map, Two pointers (same direction), Sliding window (variable) and Elementary sorts (selection, bubble, insertion).

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.

Interview problems solved with Linear search include Linear search, First unique character in a string, Find the maximum, Contains duplicate on tiny input, Missing number, Find all indices of a value and Majority element.

  • 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?

The target complexity of Linear search is O(n).

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

Open the free lesson on this pattern