Back To Tutorials
Implement Linear Search Algorithm in JavaScript
Beginnerchallenge
In this challenge, you will implement the Linear Search algorithm in JavaScript. Linear search is the simplest search algorithm. It checks each element of a list sequentially until a match is found or the entire list has been searched.
You will write a function that takes an array and a target value as input and returns the index of the target if it is found, or -1 if it is not found.
Objective:
- Implement a linear search function that searches for a given target in an array and returns its index or
-1
if the target is not present.
Requirements:
- Write a function
linearSearch(arr, target)
that:- Accepts an array
arr
and atarget
value. - Returns the index of the target if found.
- Returns
-1
if the target is not found in the array.
- Accepts an array
- The algorithm should check each element of the array one by one, making it a linear search with a time complexity of O(n).
Steps:
-
Function Definition:
- Define the function
linearSearch(arr, target)
.
- Define the function
-
Iterate Over the Array:
- Use a
for
loop to iterate over each element in the array. - During each iteration, compare the current element with the target value.
- Use a
-
Return the Index:
- If the current element matches the target, return its index.
- If the loop finishes and no match is found, return
-1
.
-
Edge Cases:
- Handle cases where the array is empty by returning
-1
. - Test with an array containing multiple occurrences of the target and return the first match.
- Ensure the function works with both numeric and string arrays.
- Handle cases where the array is empty by returning
Resources:
Solution:
Here’s the solution for the linear search implementation in JavaScript:
// Linear search function
function linearSearch(arr, target) {
// Iterate over each element in the array
for (let i = 0; i < arr.length; i++) {
// If the current element matches the target, return the index
if (arr[i] === target) {
return i;
}
}
// If target is not found, return -1
return -1;
}
// Example Usage
const array = [10, 23, 45, 70, 34, 50];
const target = 45;
console.log(linearSearch(array, target)); // Output: 2
console.log(linearSearch(array, 100)); // Output: -1
Explanation:
linearSearch():
- The function takes two parameters: an array
arr
and a target value. - It uses a
for
loop to iterate through each element in the array. - When it finds an element equal to the target value, it returns the current index.
- If the loop finishes and the target is not found, it returns
-1
.
Edge Cases:
- If the array is empty, the function immediately returns
-1
. - If the target is not present in the array, the function returns
-1
. - If there are multiple occurrences of the target, the function returns the index of the first occurrence.
Time Complexity:
- The time complexity of the linear search algorithm is O(n), where
n
is the number of elements in the array. This is because, in the worst case, every element must be checked once.