> For the complete documentation index, see [llms.txt](https://kdongs.gitbook.io/kdocs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdongs.gitbook.io/kdocs/search/linear-search.md).

# 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.

{% hint style="info" %}
This will be $$O(N)$$, since in the worst case your desired value will be at the last position, or is not found.
{% endhint %}

#### Implementation on Arrays

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