ts-ds / linearSearch
Function: linearSearch()
ts
function linearSearch<T>(
array,
target,
compareFn?
): number;Defined in: search/search.ts:17
Finds the first element equal to target by checking every element from the left.
Works on any array, sorted or not.
| Time: best | Time: average | Time: worst | Memory |
|---|---|---|---|
| O(1) | O(n) | O(n) | O(1) |
Type Parameters
| Type Parameter | Description |
|---|---|
T |
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
array | readonly T[] | undefined | Array to search. |
target | T | undefined | Value to find. |
compareFn? | CompareFn<T> | defaultCompare | Returns 0 for equal values. |
Returns
number
Index of the first equal element, or -1.