What if you could find anything in a messy list without missing a single item?
Why Linear Search Algorithm in DSA Javascript?
Imagine you have a messy drawer full of different colored socks, and you need to find a red sock. You start picking up each sock one by one, checking its color until you find the red one.
Searching this way can take a long time if the drawer is big. You might miss the sock or get tired, making mistakes. It's slow and frustrating to check every single sock manually.
The linear search algorithm does exactly what you do but faster and without mistakes. It checks each item in a list one by one until it finds the target, making the search simple and reliable.
const socks = ['blue', 'green', 'yellow', 'red', 'black']; let found = false; for (let i = 0; i < socks.length; i++) { if (socks[i] === 'red') { found = true; break; } }
function linearSearch(items, target) {
for (let index = 0; index < items.length; index++) {
if (items[index] === target) {
return index;
}
}
return -1;
}It lets you quickly find any item in a list, no matter how messy or unordered it is.
Finding a friend's name in a list of contacts on your phone when you don't know if the list is sorted.
Linear search checks items one by one until it finds the target.
It works on any list, sorted or not.
It's simple but can be slow for very large lists.
Practice
Linear Search Algorithm?Solution
Step 1: Understand linear search process
Linear search checks each element in order, from start to end.Step 2: Compare with other search methods
Binary search divides list, hash uses extra memory, linear is simple sequential check.Final Answer:
Check each element one by one until the target is found or list ends -> Option AQuick Check:
Linear search = sequential check [OK]
- Confusing linear search with binary search
- Thinking linear search requires sorted list
- Assuming linear search uses extra memory
arr?Solution
Step 1: Understand array indexing in JavaScript
Arrays start at index 0 and go up to length - 1.Step 2: Choose loop condition to avoid out-of-bound error
Loop must run while i is less than arr.length, not equal or greater.Final Answer:
for (let i = 0; i < arr.length; i++) -> Option BQuick Check:
Loop runs from 0 to length-1 [OK]
- Using <= instead of < causing out-of-bound errors
- Starting loop from 1 instead of 0
- Confusing array length with last index
const arr = [4, 8, 15, 16, 23, 42];
function linearSearch(array, target) {
for (let i = 0; i < array.length; i++) {
if (array[i] === target) {
return i;
}
}
return -1;
}
console.log(linearSearch(arr, 15));Solution
Step 1: Trace the loop to find target 15
Indices: 0=4, 1=8, 2=15. At i=2, array[i] === 15 is true.Step 2: Return index when target found
Function returns 2 immediately when target matches.Final Answer:
2 -> Option AQuick Check:
Index of 15 in array = 2 [OK]
- Returning value instead of index
- Returning -1 even if target exists
- Off-by-one errors in index
function linearSearch(arr, target) {
for (let i = 0; i <= arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}Solution
Step 1: Identify loop condition error
Loop runs while i <= arr.length, which causes out-of-bound access at i = arr.length.Step 2: Fix loop condition to prevent error
Change condition to i < arr.length to stay within valid indices.Final Answer:
Change loop condition to i < arr.length -> Option CQuick Check:
Loop must stay within array bounds [OK]
- Using <= instead of < in loop condition
- Thinking == vs === causes runtime error here
- Adding break after return (unnecessary)
[3, 5, 7, 7, 9] using linear search. Which code snippet correctly returns the index of the first 7?Solution
Step 1: Understand requirement to find first occurrence
Start from index 0 and return index when first 7 is found.Step 2: Analyze each option for correctness
for (let i = 0; i < arr.length; i++) { if (arr[i] === 7) return i; } return -1; loops forward, returns index correctly. for (let i = arr.length - 1; i >= 0; i--) { if (arr[i] === 7) return i; } return -1; loops backward (last occurrence). for (let i = 1; i <= arr.length; i++) { if (arr[i] === 7) return i; } return -1; has wrong loop bounds. for (let i = 0; i < arr.length; i++) { if (arr[i] == 7) return arr[i]; } return -1; returns value, not index.Final Answer:
for (let i = 0; i < arr.length; i++) { if (arr[i] === 7) return i; } return -1; -> Option DQuick Check:
Forward loop returns first match index [OK]
- Looping backward returns last occurrence, not first
- Returning value instead of index
- Using wrong loop bounds causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
