Linear Search

About

The simplest form of search, just linearly walking through a DS and checking if the value in the current position is the desired value.

circle-info

This will be O(N)O(N), since in the worst case your desired value will be at the last position, or is not found.

Implementation on Arrays

function linearSearch(haystack: number[], needle: number): boolean {
    for (let i = 0; i < haystack.length; i++) {
        if (haystack[i] === needle) return true;
    }
    return false;
}

Last updated