Linear Search scans each element one by one from left to right until it finds the target value or exhausts the array. Simple and elegant — set a target below to begin.
Search Target
Quick examples:
↑ Set a target value above to begin searching the array
Loading controls...
Loading controls...
Legend
Currently Checking
Element being compared to target
Already Scanned
Checked — did not match
Found!
Target value located here
Not Found
All elements checked, no match
Not Yet Checked
Waiting to be searched
— — — target: N
Dashed line marks target height
Reading the chart:
• Bar height = element value
• Dashed line = target value level
• Numbers above bars = values
• Numbers below bars = array indices
Loading visualization...
Time Complexity: O(n)
Space Complexity: O(1)
Inspector
No active frame to inspect
Loading visualization...
Time: O(n)
Space: O(1)
Legend
Currently Checking
Element being compared to target
Already Scanned
Checked — did not match
Found!
Target value located here
Not Found
All elements checked, no match
Not Yet Checked
Waiting to be searched
— — — target: N
Dashed line marks target height
Reading the chart:
• Bar height = element value
• Dashed line = target value level
• Numbers above bars = values
• Numbers below bars = array indices
Inspector
No active frame to inspect
Reference implementation of Linear Search. Select a language tab to view and copy the code.
int linear_search(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target)
return i;
}
return -1;
}
#include <vector>
int linearSearch(const std::vector<int>& arr, int target) {
for (int i = 0; i < static_cast<int>(arr.size()); i++) {
if (arr[i] == target)
return i;
}
return -1;
}
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target)
return i;
}
return -1;
}
def linear_search(arr: list[int], target: int) -> int:
for i, val in enumerate(arr):
if val == target:
return i
return -1
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}
Python — Code Walkthrough
1
enumerate — index and value together
for i, val in enumerate(arr)
enumerate yields (index, value) pairs, so both i and val are available without arr[i]. This is more idiomatic Python than a manual range(len(arr)) loop.
2
Direct value comparison
if val == target: return i
Compares the current value to target. Returning i immediately short-circuits the loop — Python will not check any remaining elements.
3
Return -1 after the loop
return -1
Python has no break-with-value syntax; the -1 return after the for loop body is reached only when the loop completes without finding target.
Function signature int linear_search(int arr[], int n, int target) Returns the index of target if found, or -1 otherwise. Because C arrays decay to pointers, the size n must be passed explicitly — there is no built-in length field.Single pass through the array for (int i = 0; i < n; i++) Visits every element from index 0 to n−1 in order. No sorting or preprocessing is required — the array can be in any order.Element comparison if (arr[i] == target) return i; As soon as a match is found, the index i is returned immediately. This early exit makes best-case performance O(1) when the target is the first element.Not-found sentinel return -1; -1 is the universally understood "not found" value since it is never a valid array index. The caller checks the return value against -1.
const reference — no copy, no mutation const std::vector<int>& arr Passing by const reference avoids copying the vector and signals that the function will not modify it. This is idiomatic C++ for read-only parameters.Size cast to int static_cast<int>(arr.size()) vector::size() returns an unsigned size_t. Casting to int prevents signed/unsigned comparison warnings when compared against the signed loop counter i.Early return on match if (arr[i] == target) return i; The function returns as soon as the target is found, avoiding unnecessary comparisons. In the worst case (not found or last element) all n elements are visited.Return -1 if not found return -1; Reached only if the entire vector was scanned without a match. The caller should compare the result against -1 (or ≥ 0) before using it as an index.
Static helper method public static int linearSearch(int[] arr, int target) Static so it can be called without an instance. Java primitive int[] arrays are passed by reference, so no copy is made.arr.length — built-in field for (int i = 0; i < arr.length; i++) Java arrays carry their length as a built-in .length field — no separate size parameter needed unlike C.Return index on match if (arr[i] == target) return i; Returns immediately when a match is found. The == operator compares int primitives by value, so this is a straightforward equality check.-1 as the not-found sentinel return -1; Java convention for "index not found" (-1 is also used by String.indexOf and List.indexOf for the same reason).
arr.length in the loop condition for (let i = 0; i < arr.length; i++) arr.length is accessed each iteration. For a simple linear search this is fine; if the array could change during iteration, caching the length would be safer.Strict equality === if (arr[i] === target) return i; Uses strict equality (===) rather than loose equality (==) to avoid unexpected type coercion. For example, 1 == "1" is true but 1 === "1" is false.Return -1 for not found return -1; Mirrors the same convention as Array.prototype.indexOf, making linear search results easy to integrate with existing JS patterns like if (result !== -1).