Visit Sponsor

Written by 2:35 pm Core Java

Linear Sequential Search in Java – Example & Code Guide

Linear sequential search is one of the fundamental algorithms in computer science. It scans each element of a collection one by one until it finds the target value. While not the most efficient for large datasets, it remains important for understanding algorithm design and learning how basic search logic works.

This updated guide on javatechig.com explains how to implement a linear sequential search in Java, explore performance characteristics, handle edge cases, and review best practices for real‑world use.

What Is Linear Sequential Search?

Linear search — also known as sequential search — checks each element in a list sequentially until the target is found or the list ends.

It works on:

  • Arrays
  • Lists (ArrayList, LinkedList)
  • Any iterable collection

Unlike binary search, it does not require sorted data.

Time and Space Complexity

MetricComplexity
Time (Best case)O(1)
Time (Average & Worst)O(n)
SpaceO(1)

Linear search is simple but becomes inefficient on large datasets compared to divide‑and‑conquer algorithms like binary search.

Array Linear Search Example (Primitive Types)

Java Code

public static int linearSearch(int[] arr, int target) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == target) {
            return i; // Found at index i
        }
    }
    return -1; // Not found
}

public static void main(String[] args) {
    int[] numbers = {11, 23, 9, 32, 5};
    int result = linearSearch(numbers, 32);

    if (result != -1) {
        System.out.println("Element found at index: " + result);
    } else {
        System.out.println("Element not found");
    }
}

This example checks each element until the target (32) is found.

ArrayList Linear Search Example

Java Example

import java.util.ArrayList;

public class LinearSearchExample {

    public static int linearSearch(ArrayList<String> list, String target) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(target)) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<>();
        items.add("Java");
        items.add("Python");
        items.add("C++");

        int index = linearSearch(items, "Python");
        System.out.println("Found at index: " + index);
    }
}

This searches an ArrayList<String> for a matching value.

Linear Search with Generics

To search any object type, use Java generics:

Java Example

public static <T> int genericLinearSearch(T[] array, T target) {
    for (int i = 0; i < array.length; i++) {
        if (array[i].equals(target)) {
            return i;
        }
    }
    return -1;
}

This method works for Integer[], String[], and custom objects with .equals() properly implemented.

Handling Edge Cases

Empty Array

if (arr.length == 0) {
    return -1;
}

No match is possible — return -1.

Null Value Check

if (target == null) {
    throw new IllegalArgumentException("Target must not be null");
}

Guard against null to avoid NullPointerException.

When to Use Linear Search

Use linear search when:

  • Dataset is small
  • Data is unsorted
  • Simplicity is more important than performance

For large sorted datasets, binary search is substantially faster.

Differences from Binary Search

FeatureLinear SearchBinary Search
Requires SortedNoYes
Time (Worst)O(n)O(log n)
SimplicityVery simpleModerate
Use CasesSmall/unsortedLarge/sorted

Best Practices (2026 Updated)

  • Use Java Streams for expressive filtering:
OptionalInt index = IntStream.range(0, arr.length)
    .filter(i -> arr[i] == target)
    .findFirst();
  • Prefer built‑in search utilities if available
  • For object collections, ensure proper equals() override
  • Use binary search when data is sorted
Visited 9 times, 1 visit(s) today
Close