Data Structure Linear Search Practice Questions with Solutions

Introduction

Linear Search is one of the simplest searching techniques used in Data Structures. It checks each element one by one from the beginning of an array until the required value is found or the array ends. In this chapter, we will practice Linear Search using practical JavaScript examples. Each solved question focuses on a different situation, helping you understand how to implement and use Linear Search correctly. Data Structure Linear Search practice questions with solutions help to understand the concepts.

Question 1: Find an Element Using Linear Search

Questions

Write a JavaScript program to find the position of 30 in the following array using Linear Search.

let numbers = [10, 20, 30, 40, 50];
let target = 30;

Solution

let numbers = [10, 20, 30, 40, 50];
let target = 30;

let position = -1;

for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] === target) {
        position = i;
        break;
    }
}

console.log("Position:", position);

Output

Position: 2

The value 30 is found at index 2.


Question 2: Search for a Number Without Using indexOf()

Questions

Find the number 45 in an array using Linear Search without using built-in searching methods.

let numbers = [12, 25, 37, 45, 56];

Solution

let numbers = [12, 25, 37, 45, 56];
let target = 45;

let found = false;

for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] === target) {
        found = true;
        break;
    }
}

if (found) {
    console.log("Element found");
} else {
    console.log("Element not found");
}

Output

Element found


Question 3: Search for a String in an Array

Questions

Use Linear Search to find "Python" in the following array.

let courses = ["HTML", "CSS", "JavaScript", "Python", "Node.js"];

Solution

let courses = ["HTML", "CSS", "JavaScript", "Python", "Node.js"];
let target = "Python";

let position = -1;

for (let i = 0; i < courses.length; i++) {
    if (courses[i] === target) {
        position = i;
        break;
    }
}

console.log("Position:", position);

Output

Position: 3

The string "Python" is found at index 3.


Question 4: Find the First Occurrence of an Element

Questions

An array contains duplicate values. Use Linear Search to find the first position of 20.

let numbers = [10, 20, 30, 20, 40, 20];

Solution

let numbers = [10, 20, 30, 20, 40, 20];
let target = 20;

let firstPosition = -1;

for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] === target) {
        firstPosition = i;
        break;
    }
}

console.log("First position:", firstPosition);

Output

First position: 1

The search stops as soon as the first 20 is found.


Question 5: Count How Many Times an Element Appears

Questions

Use Linear Search to count how many times 5 appears in the array.

let numbers = [5, 2, 5, 8, 5, 10, 5];

Solution

let numbers = [5, 2, 5, 8, 5, 10, 5];
let target = 5;

let count = 0;

for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] === target) {
        count++;
    }
}

console.log("Occurrences:", count);

Output

Occurrences: 4

Unlike a normal search, the loop does not stop after finding the first match because we need to count every occurrence.


Question 6: Find the Last Occurrence of an Element

Questions

Use Linear Search to find the last position of 25.

let numbers = [10, 25, 30, 25, 40, 25, 50];

Solution

let numbers = [10, 25, 30, 25, 40, 25, 50];
let target = 25;

let lastPosition = -1;

for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] === target) {
        lastPosition = i;
    }
}

console.log("Last position:", lastPosition);

Output

Last position: 5

Every matching position updates lastPosition, so the final value represents the last occurrence.


Question 7: Find the Smallest Number Using Linear Traversal

Questions

Use a linear traversal to find the smallest number in the array.

let numbers = [45, 12, 78, 5, 34, 20];

Solution

let numbers = [45, 12, 78, 5, 34, 20];

let smallest = numbers[0];

for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] < smallest) {
        smallest = numbers[i];
    }
}

console.log("Smallest:", smallest);

Output

Smallest: 5

The program checks every element once and keeps updating the smallest value.


Question 8: Find the Largest Number Using Linear Traversal

Questions

Use a linear traversal to find the largest number in the array.

let numbers = [15, 72, 34, 91, 28, 60];

Solution

let numbers = [15, 72, 34, 91, 28, 60];

let largest = numbers[0];

for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] > largest) {
        largest = numbers[i];
    }
}

console.log("Largest:", largest);

Output

Largest: 91

The program compares each element with the current largest value.


Question 9: Linear Search in an Array of Objects

Questions

Use Linear Search to find the student whose ID is 103.

let students = [
    { id: 101, name: "Rahul" },
    { id: 102, name: "Priya" },
    { id: 103, name: "Aman" },
    { id: 104, name: "Neha" }
];

Solution

let students = [
    { id: 101, name: "Rahul" },
    { id: 102, name: "Priya" },
    { id: 103, name: "Aman" },
    { id: 104, name: "Neha" }
];

let targetId = 103;
let student = null;

for (let i = 0; i < students.length; i++) {
    if (students[i].id === targetId) {
        student = students[i];
        break;
    }
}

console.log(student);

Output

{ id: 103, name: 'Aman' }

This is a practical example because real applications often search arrays of objects rather than simple numbers.


Question 10: Create a Reusable Linear Search Function

Questions

Create a reusable function called linearSearch() that accepts an array and a target value. Return the index if the element is found; otherwise return -1.

Solution

function linearSearch(array, target) {
    for (let i = 0; i < array.length; i++) {
        if (array[i] === target) {
            return i;
        }
    }

    return -1;
}

let numbers = [11, 22, 33, 44, 55];

console.log(linearSearch(numbers, 44));
console.log(linearSearch(numbers, 99));

Output

3
-1

The function can now be reused with different arrays and target values.

Key Takeaways

  • Linear Search checks elements one by one.
  • It can be used with sorted as well as unsorted data.
  • The search normally starts from index 0.
  • The search can stop immediately when the target is found.
  • Returning -1 is a common way to indicate that an element was not found.
  • Linear Search can find the first occurrence or last occurrence.
  • It can also be used for counting occurrences.
  • Linear traversal can be used to find minimum and maximum values.
  • Arrays of objects can also be searched using Linear Search.
  • The worst-case time complexity of Linear Search is O(n).
  • Its best-case time complexity is O(1) when the target is the first element.

FAQs

1. What is Linear Search?

Linear Search is a searching technique that checks each element one by one until the required element is found or the complete collection has been checked.

2. What is the time complexity of Linear Search?

The worst-case time complexity is O(n), where n is the number of elements.

3. Can Linear Search work on an unsorted array?

Yes. One major advantage of Linear Search is that the array does not need to be sorted.

4. What happens when the element is not found?

A common implementation returns -1 to indicate that the target element does not exist in the array.

5. What is the best-case time complexity of Linear Search?

The best-case complexity is O(1) when the target element is found at the first position.

6. Can Linear Search find duplicate elements?

Yes. It can find the first occurrence, last occurrence, or all occurrences depending on how the loop is implemented.

7. When should Linear Search be used?

Linear Search is useful for small or unsorted collections where simplicity is more important than advanced searching performance.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top