Introductions
Arrays are one of the most important data structures for beginners because they introduce concepts such as indexing, traversal, insertion, deletion, searching, updating, and finding values. These practice questions focus on using arrays to solve problems, rather than memorizing definitions. The examples start with simple operations and gradually build problem-solving skills using JavaScript, making them suitable for beginners. Data Structure Arrays practice questions with solutions help to understand the concepts.
Question 1: Access an Element Using Its Index
Question
Given the following array:
let numbers = [10, 20, 30, 40, 50];
Find the element stored at index 3.
Solution
Array indexes start from 0.
The array is:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
The question asks for the value at index 3.
So we use:
console.log(numbers[3]);
Output
40
Answer
The element at index 3 is 40.
Question 2: Find the Length of an Array
Question
Find the number of elements in this array:
let fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"];
Solution
JavaScript provides the length property to find the number of elements in an array.
console.log(fruits.length);
The array contains:
Apple
Banana
Mango
Orange
Grapes
There are 5 elements.
Output
5
Answer
The length of the array is 5.
Question 3: Update an Array Element
Question
Given:
let marks = [65, 72, 81, 90, 76];
Change the mark at index 2 to 95.
Solution
First, identify the element at index 2.
Index: 0 1 2 3 4
Value: 65 72 81 90 76
Index 2 contains 81.
To replace it with 95, write:
marks[2] = 95;
Now the array becomes:
[65, 72, 95, 90, 76]
We can check the updated array:
console.log(marks);
Output
[65, 72, 95, 90, 76]
Answer
The value at index 2 is successfully changed from 81 to 95.
Question 4: Traverse an Array
Question
Print every element of the following array using a loop:
let numbers = [5, 10, 15, 20, 25];
Solution
To visit every element, we can use a for loop.
let numbers = [5, 10, 15, 20, 25];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
The loop starts with:
i = 0
and continues until:
i < numbers.length
The values are accessed one by one:
numbers[0] → 5
numbers[1] → 10
numbers[2] → 15
numbers[3] → 20
numbers[4] → 25
Output
5
10
15
20
25
Answer
The array has been successfully traversed using a loop.
Question 5: Find the Largest Element in an Array
Question
Find the largest number in this array:
let numbers = [12, 45, 7, 89, 34, 67];
Solution
Start by assuming the first element is the largest:
let largest = numbers[0];
So initially:
largest = 12
Now compare every remaining element with largest.
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
Let’s trace the values:
Start → largest = 12
45 > 12 → largest = 45
7 > 45 → No
89 > 45 → largest = 89
34 > 89 → No
67 > 89 → No
Finally:
largest = 89
Complete code:
let numbers = [12, 45, 7, 89, 34, 67];
let largest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
console.log(largest);
Output
89
Answer
The largest element is 89.
Question 6: Find the Smallest Element in an Array
Question
Find the smallest number in the following array:
let numbers = [25, 8, 42, 15, 3, 19];
Solution
Assume that the first element is the smallest:
let smallest = numbers[0];
Initially:
smallest = 25
Now compare each remaining element.
8 < 25 → smallest = 8
42 < 8 → No
15 < 8 → No
3 < 8 → smallest = 3
19 < 3 → No
Therefore:
smallest = 3
Complete code:
let numbers = [25, 8, 42, 15, 3, 19];
let smallest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
console.log(smallest);
Output
3
Answer
The smallest element is 3.
Question 7: Search for an Element in an Array
Question
Search for the number 30 in the following array:
let numbers = [10, 20, 30, 40, 50];
Print its index if it is found.
Solution
We can check each element using a loop.
let numbers = [10, 20, 30, 40, 50];
let target = 30;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === target) {
console.log("Found at index:", i);
break;
}
}
Let’s check the elements:
10 === 30 → No
20 === 30 → No
30 === 30 → Yes
The value 30 is located at index 2.
Output
Found at index: 2
Answer
The number 30 is found at index 2.
Question 8: Count Even Numbers in an Array
Question
Count how many even numbers are present in this array:
let numbers = [11, 24, 35, 42, 50, 63, 72];
Solution
An even number is divisible by 2.
We can check this using:
numbers[i] % 2 === 0
Start with:
let count = 0;
Then check each element:
let numbers = [11, 24, 35, 42, 50, 63, 72];
let count = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
count++;
}
}
console.log(count);
Let’s identify the even numbers:
11 → Odd
24 → Even
35 → Odd
42 → Even
50 → Even
63 → Odd
72 → Even
Even numbers are:
24, 42, 50, 72
There are 4.
Output
4
Answer
There are 4 even numbers in the array.
Question 9: Reverse an Array
Question
Reverse the following array without using the built-in reverse() method:
let numbers = [10, 20, 30, 40, 50];
Solution
We need to read the array from the last index to the first index.
The original array is:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
Start from index 4:
50
Then:
40
30
20
10
We can create a new array:
let numbers = [10, 20, 30, 40, 50];
let reversed = [];
for (let i = numbers.length - 1; i >= 0; i--) {
reversed.push(numbers[i]);
}
console.log(reversed);
The loop starts at:
numbers.length - 1
Since the length is 5:
5 - 1 = 4
So the loop starts at index 4 and moves backward.
Output
[50, 40, 30, 20, 10]
Answer
The reversed array is:
[50, 40, 30, 20, 10]
Question 10: Find the Sum and Average of Array Elements
Question
Find the sum and average of the following array:
let numbers = [10, 20, 30, 40, 50];
Solution
First, we need to calculate the sum.
Start with:
let sum = 0;
Then add each element:
sum = 0 + 10 = 10
sum = 10 + 20 = 30
sum = 30 + 30 = 60
sum = 60 + 40 = 100
sum = 100 + 50 = 150
So:
Sum = 150
Now calculate the average.
The formula is:
Average = Sum / Number of Elements
There are 5 elements.
Therefore:
Average = 150 / 5
Average = 30
Complete code:
let numbers = [10, 20, 30, 40, 50];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
let average = sum / numbers.length;
console.log("Sum:", sum);
console.log("Average:", average);
Output
Sum: 150
Average: 30
Answer
Sum = 150
Average = 30
Key Takeaways
- Array indexes generally start from
0. - Use
array[index]to access an element. - Use
array.lengthto find the number of elements. - Array elements can be updated using their indexes.
- Traversing means visiting array elements one by one.
- A loop can be used to search for a particular value.
- The largest and smallest elements can be found by comparing values.
- The
%operator can help identify even and odd numbers. - An array can be reversed by traversing it from the last index to the first.
- Array elements can be used to calculate values such as sum and average.
- Most basic array traversal problems require O(n) time because each element may need to be visited.
FAQs
1. What is an array in data structures?
An array is a data structure used to store multiple elements in an ordered collection, where elements can generally be accessed using indexes.
2. What is the first index of an array in JavaScript?
The first index of a JavaScript array is 0.
3. How do I find the length of an array?
Use the length property:
console.log(arr.length);
4. How do I access an array element?
Use its index:
console.log(arr[2]);
This accesses the element stored at index 2.
5. What is array traversal?
Array traversal means visiting or processing each element of an array, usually with a loop.
6. What is the time complexity of searching an unsorted array?
A basic linear search has O(n) time complexity in the worst case because it may need to check every element.
7. Why should beginners practice array problems?
Array problems build important programming skills such as loops, indexing, conditions, searching, comparison, counting, and problem-solving. These skills are also useful when learning other data structures.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
