Linear Search
Linear Search (or sequential search) is the simplest search algorithm. It sequentially checks each element of the list until a match is found or the whole list has been searched.
### How it works: 1. Start from the leftmost element of the array and one by one compare the target with each element. 2. If the current element matches the target, return its index. 3. If the loop finishes executing without finding the target, return `-1`.
Linear search is rarely used in real-world applications for large datasets because of its `O(n)` complexity, but it is useful when working with unsorted arrays where faster algorithms like Binary Search cannot be applied.
### How it works: 1. Start from the leftmost element of the array and one by one compare the target with each element. 2. If the current element matches the target, return its index. 3. If the loop finishes executing without finding the target, return `-1`.
Linear search is rarely used in real-world applications for large datasets because of its `O(n)` complexity, but it is useful when working with unsorted arrays where faster algorithms like Binary Search cannot be applied.
Constraints
- 1 <= arr.length <= 10^4
- -10^5 <= arr[i], target <= 10^5
TimeO(O(n))
SpaceO(O(1))
Ready to startStep 0 / 0
TypeScript