Linear Search is one of the simplest and most beginner-friendly algorithms for finding an element in a list or array. The idea is very straightforward: start at the beginning of the array and check each element one by one until you find the target value. Because of its simplicity, Linear Search is a great starting point for anyone learning TypeScript and basic algorithms.
with hands-on learning.
get the skills and confidence to land your next move.
Although it may not be the fastest search method for large datasets compared to algorithms like Binary Search, Linear Search has the advantage of working on any array, whether it’s sorted or unsorted. It is widely used in situations where the dataset is small, dynamic, or unsorted, such as looking up a name in a list of students or checking for a specific value in a log file. Understanding Linear Search also lays the foundation for learning more advanced searching algorithms in the future.
Program 1: Linear Search Using a For Loop
This program demonstrates the classic Linear Search approach using a simple for loop. It searches through an array to find the position of a target value.
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // Return the index where the target is found
}
}
return -1; // Return -1 if target is not found
}
let numbers: number[] = [10, 20, 30, 40, 50];
let targetValue: number = 30;
console.log("Element found at index:", linearSearch(numbers, targetValue));In this example, the function checks each element of the array one by one. When it finds the target, it returns the index. Beginners can visualize it as scanning each item until the correct one is found. This method is intuitive and easy to implement.
Program 2: Linear Search Using a While Loop
This version shows how Linear Search can be implemented using a while loop instead of a for loop.
function linearSearchWhile(arr: number[], target: number): number {
let i = 0;
while (i < arr.length) {
if (arr[i] === target) {
return i;
}
i++;
}
return -1;
}
let values: number[] = [5, 15, 25, 35, 45];
let searchValue: number = 25;
console.log("Element found at index:", linearSearchWhile(values, searchValue));Using a while loop helps beginners understand alternative control structures. The logic remains the same, but the way we iterate over the array changes, giving flexibility in programming.
Program 3: Linear Search Using Recursion
Linear Search can also be implemented recursively, which is a good way to practice recursion for beginners.
function linearSearchRecursive(arr: number[], target: number, index: number = 0): number {
if (index >= arr.length) return -1;
if (arr[index] === target) return index;
return linearSearchRecursive(arr, target, index + 1);
}
let items: number[] = [2, 4, 6, 8, 10];
let target: number = 8;
console.log("Element found at index:", linearSearchRecursive(items, target));This program works by checking the current element and then calling itself for the next index if the target is not found. It’s useful for beginners to understand how recursion can replace loops in certain situations.
Program 4: Linear Search with Duplicate Values
This program demonstrates how Linear Search can handle arrays with duplicate elements and find the first occurrence.
function linearSearchFirstDuplicate(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}
let duplicateArray: number[] = [1, 2, 3, 2, 4, 2];
let duplicateTarget: number = 2;
console.log("First occurrence found at index:", linearSearchFirstDuplicate(duplicateArray, duplicateTarget));Linear Search returns the index of the first match, which makes it easy for beginners to see how it works even when there are repeated values in the array.
Program 5: Linear Search in an Array of Strings
Linear Search is not limited to numbers. This program searches through an array of strings.
function linearSearchString(arr: string[], target: string): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}
let names: string[] = ["Harry", "Hermione", "Ron", "Luna"];
let searchName: string = "Ron";
console.log("Element found at index:", linearSearchString(names, searchName));Beginners can understand that Linear Search works with any data type as long as you can compare elements for equality.
Program 6: Linear Search with Negative Numbers
Linear Search can handle negative numbers just as easily as positive numbers.
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // Return the index where the target is found
}
}
return -1; // Return -1 if target is not found
}
let negatives: number[] = [-10, -5, 0, 5, 10];
let negativeTarget: number = -5;
console.log("Element found at index:", linearSearch(negatives, negativeTarget));This program reassures beginners that no special logic is required for negative numbers.
Program 7: Linear Search with Floating Point Numbers
This program searches for a decimal value in an array.
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // Return the index where the target is found
}
}
return -1; // Return -1 if target is not found
}
let decimals: number[] = [1.1, 2.2, 3.3, 4.4];
let decimalTarget: number = 3.3;
console.log("Element found at index:", linearSearch(decimals, decimalTarget));Linear Search works naturally with floating point numbers in TypeScript, making it versatile for different numeric datasets.
Program 8: Linear Search for Unsorted Arrays
Linear Search works equally well on unsorted arrays.
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // Return the index where the target is found
}
}
return -1; // Return -1 if target is not found
}
let unsorted: number[] = [12, 7, 9, 3, 15];
let searchUnsorted: number = 9;
console.log("Element found at index:", linearSearch(unsorted, searchUnsorted));Beginners can see that unlike Binary Search, Linear Search does not require the array to be sorted. This is one of its major advantages for small or unsorted datasets.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Linear Search in TypeScript.
Q1. Is Linear Search fast for large arrays?
Linear Search is simple but not very fast for large datasets since it may check every element.
Q2. Can Linear Search handle duplicates?
Yes, it returns the first occurrence of the target element.
Q3. Does Linear Search require the array to be sorted?
No, it works on both sorted and unsorted arrays.
Q4. Can Linear Search be used with strings and decimals?
Yes, as long as the elements can be compared for equality.
Q5. Is Linear Search beginner-friendly?
Yes, its simplicity makes it perfect for learning basic searching algorithms and understanding array traversal.
Conclusion
Linear Search is a simple and intuitive algorithm that every beginner should learn. In this article, we explored several TypeScript programs demonstrating Linear Search using loops, recursion, and different data types including numbers, strings, negatives, and decimals. Each program showed how the algorithm scans through an array to find the target element, making it easy to understand and implement.
By practicing Linear Search, beginners gain confidence in array handling, conditional checks, and basic algorithmic thinking. It is a stepping stone toward more advanced searching techniques like Binary Search and Hashing, and mastering it will give you a strong foundation in programming logic.




