Skip to content

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: bestTime: averageTime: worstMemory
O(1)O(n)O(n)O(1)

Type Parameters ​

Type ParameterDescription
T

Parameters ​

ParameterTypeDefault valueDescription
arrayreadonly T[]undefinedArray to search.
targetTundefinedValue to find.
compareFn?CompareFn<T>defaultCompareReturns 0 for equal values.

Returns ​

number

Index of the first equal element, or -1.

MIT License