Traverse an Array
Problem Statement
Given an array of elements, the task is to traverse the array and access each element one by one. Traversing means visiting every element of the array sequentially, usually from the first index to the last.
Array traversal is the foundation of almost all array-based algorithms. Any operation such as searching, sorting, updating, or counting elements requires traversal.
Understanding Array Traversal
An array stores elements in contiguous memory locations and each element can be accessed using its index.
If:
arr = [10, 20, 30, 40]
Indexes will be:
Index: 0 1 2 3
Value: 10 20 30 40
To traverse this array, we:
- Start from index 0
- Move sequentially till n-1
- Access each element using arr[index]
Algorithm (Step-by-Step)
- Start from index 0
- Repeat until index is less than array size
- Print or process the current element
- Move to the next index
Pseudocode
for i from 0 to n-1:
print arr[i]
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |
Traversal requires visiting every element once.
Language-wise Implementation
C Implementation
#include
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
10 20 30 40 50
C++ Implementation
#include
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output
10 20 30 40 50
Java Implementation
public class Main {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output
10 20 30 40 50
Python Implementation
arr = [10, 20, 30, 40, 50]
for i in range(len(arr)):
print(arr[i], end=" ")
Output
10 20 30 40 50
C# Implementation
using System;
class Program {
static void Main() {
int[] arr = {10, 20, 30, 40, 50};
for(int i = 0; i < arr.Length; i++) {
Console.Write(arr[i] + " ");
}
}
}
Output
10 20 30 40 50
JavaScript Implementation
let arr = [10, 20, 30, 40, 50];
for (let i = 0; i < arr.length; i++) {
process.stdout.write(arr[i] + " ");
}
Output
10 20 30 40 50
Alternate Traversal Methods
Using Enhanced Loops
| Language | Syntax |
|---|---|
| C++ | range-based for loop |
| Java | for-each loop |
| Python | direct iteration |
| JavaScript | for...of |
Example (Java):
for(int num : arr) {
System.out.print(num + " ");
}
Common Mistakes
- Accessing index out of bounds
- Using incorrect loop condition
- Forgetting array length calculation
- Hardcoding array size
- Modifying loop variable inside loop body
Real-World Use Cases
- Printing array elements
- Searching for an element
- Counting frequency
- Computing sum or average
- Validating array properties
Summary
Traversing an array is the most fundamental array operation and acts as the backbone for all array-based algorithms. By understanding traversal clearly, you build a strong foundation for solving complex problems involving searching, sorting, sliding windows, prefix sums, and more.
Mastering array traversal ensures clarity in logic, efficiency in implementation, and confidence in handling more advanced data structure problems.
