Introductions
Searching is the process of finding a specific element or value inside a data structure. Different searching techniques are useful in different situations. Linear Search checks elements one by one, while Binary Search repeatedly divides a sorted array into smaller parts. In this chapter, you will practice searching through 10 solved JavaScript questions covering basic searching, indexes, duplicates, sorted arrays, and practical search conditions. Data Structure Searching practice questions with solutions help to understand the concepts.
Question 1: Find an Element Using Linear Search
Question
Given the following array, search for the value 40 using Linear Search.
const numbers = [10, 20, 30, 40, 50];
Solution
Linear Search checks every element from left to right until the required value is found.
const numbers = [10, 20, 30, 40, 50];
const target = 40;
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
console.log(linearSearch(numbers, target));
Output
3
Answer
The value 40 is found at index 3.
Question 2: Search for a String in an Array
Question
Search for "Python" in the following array and return its index.
const languages = [
"JavaScript",
"Java",
"Python",
"C++",
"PHP"
];
Solution
const languages = [
"JavaScript",
"Java",
"Python",
"C++",
"PHP"
];
const target = "Python";
function searchLanguage(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
console.log(searchLanguage(languages, target));
Output
2
Answer
Python is located at index 2.
Question 3: Check Whether an Element Exists
Question
Check whether the number 75 exists in the following array.
const numbers = [15, 25, 35, 45, 55, 65];
Return true if it exists and false otherwise.
Solution
const numbers = [15, 25, 35, 45, 55, 65];
function contains(arr, target) {
for (const value of arr) {
if (value === target) {
return true;
}
}
return false;
}
console.log(contains(numbers, 75));
Output
false
Answer
75 does not exist in the array, so the result is false.
Question 4: Find the First Occurrence of a Duplicate Value
Question
The array contains duplicate values:
const numbers = [10, 20, 30, 20, 40, 20];
Find the first index where 20 appears.
Solution
Stop searching immediately after finding the first matching value.
const numbers = [10, 20, 30, 20, 40, 20];
function firstOccurrence(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
console.log(firstOccurrence(numbers, 20));
Output
1
Answer
The first occurrence of 20 is at index 1.
Question 5: Find All Occurrences of an Element
Question
Find all indexes where 5 appears.
const numbers = [5, 2, 5, 7, 5, 9];
Solution
Instead of stopping at the first match, continue searching through the complete array.
const numbers = [5, 2, 5, 7, 5, 9];
function findAllOccurrences(arr, target) {
const indexes = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
indexes.push(i);
}
}
return indexes;
}
console.log(findAllOccurrences(numbers, 5));
Output
[ 0, 2, 4 ]
Answer
The value 5 occurs at indexes:
0, 2, 4
Question 6: Perform Binary Search on a Sorted Array
Question
Search for 70 using Binary Search.
const numbers = [10, 20, 30, 40, 50, 60, 70, 80];
Solution
Binary Search works on a sorted array. It repeatedly checks the middle element and eliminates half of the search area.
const numbers = [10, 20, 30, 40, 50, 60, 70, 80];
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
console.log(binarySearch(numbers, 70));
Output
6
Answer
The value 70 is found at index 6.
Question 7: Binary Search When the Element Is Missing
Question
Use Binary Search to find 45 in:
const numbers = [10, 20, 30, 40, 50, 60, 70];
If the value does not exist, return -1.
Solution
const numbers = [10, 20, 30, 40, 50, 60, 70];
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
console.log(binarySearch(numbers, 45));
Output
-1
Answer
45 is not present in the array, so the function returns -1.
Question 8: Find the First Position of a Duplicate Using Binary Search
Question
The sorted array contains duplicate values:
const numbers = [10, 20, 20, 20, 30, 40];
Find the first index of 20 using Binary Search.
Solution
When 20 is found, continue searching toward the left to check whether another 20 exists earlier.
const numbers = [10, 20, 20, 20, 30, 40];
function firstOccurrence(arr, target) {
let left = 0;
let right = arr.length - 1;
let answer = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
answer = mid;
right = mid - 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return answer;
}
console.log(firstOccurrence(numbers, 20));
Output
1
Answer
The first occurrence of 20 is at index 1.
Question 9: Find the Position Where an Element Should Be Inserted
Question
Given this sorted array:
const numbers = [10, 20, 30, 40, 50];
Find the index where 35 should be inserted while keeping the array sorted.
Solution
Binary Search can be modified to find the first position where the target can be placed.
const numbers = [10, 20, 30, 40, 50];
function searchInsertPosition(arr, target) {
let left = 0;
let right = arr.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
console.log(searchInsertPosition(numbers, 35));
Output
3
Answer
35 should be inserted at index 3:
[10, 20, 30, 35, 40, 50]
Question 10: Search for a Student by ID
Question
Given the following student records, search for student ID 103.
const students = [
{ id: 101, name: "Rahul" },
{ id: 102, name: "Priya" },
{ id: 103, name: "Aman" },
{ id: 104, name: "Neha" }
];
Return the complete student object when the ID is found.
Solution
const students = [
{ id: 101, name: "Rahul" },
{ id: 102, name: "Priya" },
{ id: 103, name: "Aman" },
{ id: 104, name: "Neha" }
];
function findStudent(students, id) {
for (const student of students) {
if (student.id === id) {
return student;
}
}
return null;
}
console.log(findStudent(students, 103));
Output
{ id: 103, name: 'Aman' }
Answer
Student ID 103 belongs to Aman.
Key Takeaways
- Searching means finding a required element inside a data structure.
- Linear Search checks elements one by one.
- Linear Search can work on both sorted and unsorted arrays.
- Binary Search requires a sorted array.
- Binary Search repeatedly reduces the search range.
- Searching can return an index, Boolean value, object, or another result.
- A search can be modified to find the first occurrence of duplicates.
- You can continue searching to find all occurrences of a value.
- Binary Search can also find the correct insertion position.
- Searching can be applied to real-world data such as student records.
FAQs
1. What is searching in data structures?
Searching is the process of locating a specific element or value inside a data structure.
2. What is Linear Search?
Linear Search checks elements one by one from the beginning until the target is found or the complete collection has been searched.
3. What is Binary Search?
Binary Search is a searching algorithm that repeatedly divides a sorted search space into smaller parts.
4. Does Binary Search require a sorted array?
Yes. Standard Binary Search requires the data to be arranged in sorted order.
5. Which is simpler, Linear Search or Binary Search?
Linear Search is generally easier to understand and implement, especially for beginners.
6. Can searching be performed on objects?
Yes. You can search objects by checking a specific property such as id, name, or email.
7. What happens when a search element is not found?
A function can return a special value such as -1, false, null, or another appropriate result to indicate that the element was not found.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
