What if you could find anything in a messy pile without sorting it first?
Why Linear Search Algorithm in DSA Typescript?
Imagine you have a messy drawer full of different colored socks. You want to find a red sock, but you have no idea where it is. So, you start pulling out socks one by one from the top until you find the red one.
Searching like this by hand is slow and tiring. If the red sock is at the bottom, you waste time checking every single sock. You might also lose track or miss the sock if you get distracted.
The linear search algorithm works just like checking each sock one by one, but it does it quickly and without mistakes. It goes through each item in a list until it finds the one you want or reaches the end.
let socks = ['blue', 'green', 'yellow', 'red', 'black']; for (let i = 0; i < socks.length; i++) { if (socks[i] === 'red') { console.log('Found red sock at position', i); break; } }
function linearSearch(items: string[], target: string): number {
for (let index = 0; index < items.length; index++) {
if (items[index] === target) {
return index;
}
}
return -1;
}
const position = linearSearch(['blue', 'green', 'yellow', 'red', 'black'], 'red');
console.log('Found red sock at position', position);Linear search lets you quickly find any item in a list without needing it to be sorted or organized.
When you look for a contact name in your phone's unsorted list, your phone uses a linear search to find the name by checking each contact one by one.
Linear search checks each item one by one until it finds the target.
It works on any list, sorted or not.
It is 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, to find the target.Step 2: Compare with other search methods
Binary search divides list, hash uses extra memory, but linear search is simple sequential check.Final Answer:
Check each element one by one until the target is found or list ends -> Option BQuick Check:
Linear search = sequential check [OK]
- Confusing linear search with binary search
- Thinking linear search requires sorted list
- Assuming linear search is always fastest
arr?Solution
Step 1: Identify correct loop start and end
Array indices start at 0 and go up to length-1, so loop must start at 0 and run while i < length.Step 2: Check each option's loop condition
for (let i = 0; i < arr.length; i++) uses i=0 and i < arr.length, which is correct. Others either start at 1 or use <= which causes out-of-bounds.Final Answer:
for (let i = 0; i < arr.length; i++) -> Option AQuick Check:
Loop from 0 to length-1 [OK]
- Starting loop at 1 instead of 0
- Using <= instead of < causing index errors
- Confusing array length with last index
const arr = [4, 7, 1, 9];
const target = 1;
let index = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
index = i;
break;
}
}
console.log(index);Solution
Step 1: Trace the loop and compare elements
Check arr[0]=4 (not 1), arr[1]=7 (not 1), arr[2]=1 (match found), set index=2 and break loop.Step 2: Output the index variable
After loop, index is 2, so console.log prints 2.Final Answer:
2 -> Option CQuick Check:
Target found at index 2 [OK]
- Not breaking loop and returning last index
- Confusing index with value
- Assuming index starts at 1
const arr = [5, 3, 8];
const target = 3;
let index = -1;
for (let i = 0; i <= arr.length; i++) {
if (arr[i] === target) {
index = i;
break;
}
}
console.log(index);Solution
Step 1: Identify loop boundary error
The loop uses i <= arr.length, which causes i to reach arr.length, an invalid index (out of bounds).Step 2: Fix loop condition to prevent out-of-bounds
Changing condition to i < arr.length ensures i stays within valid indices 0 to length-1.Final Answer:
Change loop condition to i < arr.length -> Option AQuick Check:
Loop must run while i < length [OK]
- Using <= causing runtime error
- Changing unrelated variables
- Removing break causing unnecessary iterations
const arr = [2, 5, 3, 5, 7];
const target = 5;
let index = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
index = i;
break;
}
}
console.log(index);Solution
Step 1: Understand current code behavior
The loop checks all elements; each time target matches, index updates to current i, so last match index remains after loop.Step 2: Identify why removing break helps
If break was present, loop would stop at first match. Removing break ensures full array is checked for last occurrence.Final Answer:
Remove the break statement to continue searching all elements -> Option DQuick Check:
Keep searching to find last occurrence [OK]
- Adding break stops search too early
- Sorting changes original order and index meaning
- Starting loop from end requires different logic
Ready to prove it?
Pass the test and this pattern moves unknown
