Previous topic on Array - Introduction to Array
https://www.dsawithpiyush.com/post/array-data-structure-complete-guide-for-dsa-and-coding-interviews
Array traversal is one of the most fundamental concepts in data structures.
Before solving advanced problems, understanding how to correctly traverse an array is mandatory.
If you get array traversal right, 50% of array problems automatically become easier.
What Is Array Traversal?
Array traversal means visiting each element of the array one by one to perform some operation like
→ Reading values
→ Updating values
→ Checking conditions
→ Building results (sum, count, max, etc.)In simple terms, traversal is moving through the array using indexes.
How Array Traversal Works
Arrays are index-based data structures.
Indexing starts from 0 and ends at n - 1, where n is the length of the array.
Traversal follows a simple pattern
→ Start from the first element
→ Move one step at a time
→ Stop at the last element
This guarantees that every element is processed exactly once.
Basic Array Traversal Example (JavaScript)
const arr = [10, 20, 30, 40, 50];for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}What’s happening here?
✓ i starts from 0
✓ arr[i] accesses the element at index i
✓ Loop runs until i < arr.length✓ Each element is printed once
This is the most common traversal pattern used in interviews.
Array Traversal Using for...of Loop
Sometimes, you don’t need the index — only the values.
const arr = [10, 20, 30, 40, 50];for (const value of arr) {
console.log(value);
}✓ Cleaner syntax
✓ Easy to read
✓ Useful when index position is not required
Array Traversal Example (Java)
int[] arr = {10, 20, 30, 40, 50};for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}Same logic.
Same traversal.
Only the language syntax changes.
Common Mistakes During Array Traversal
Many beginners make small mistakes that cause runtime errors or wrong answers.
❌ Forgetting to update the loop variable
for (let i = 0; i < arr.length; ) {
console.log(arr[i]);
}This leads to an infinite loop.
❌ Using incorrect loop condition
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}This causes out-of-bounds access.
✓ Correct condition is always
i < arr.lengthWhy Array Traversal Is Important for Interviews
Most interview problems are disguised traversal problems.
Traversal is used when
→ Finding maximum or minimum element
→ Counting frequency of elements
→ Checking conditions on each element
→ Building prefix sums
→ Solving sliding window problems
Even when the problem looks complex, the base logic often involves traversal.
Simple Interview-Style Example
Problem
Find the sum of all elements in an array.
Solution (JavaScript)
const arr = [1, 2, 3, 4, 5];
let sum = 0;for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}console.log(sum); // 15This pattern — traversal + accumulation — appears everywhere.
How to Practice Array Traversal Effectively
To truly master array traversal
✓ Practice writing loops without mistakes
✓ Dry run every loop manually
✓ Focus on index movement
✓ Solve beginner problems first
On DSAwithPiyush.com, array traversal problems are grouped topic-wise so you can practice in a structured and guided manner. check this Watch the video on Array -
https://www.dsawithpiyush.com/course/dsa/Arrays/ARRAYS_TRAVERSAL
Next Topic To Read -
https://www.dsawithpiyush.com/post/prefix-sum-explained-with-real-examples-dsa
Follow consistently and you’ll build strong fundamentals very fast. DSA WITH PIYUSH | DWP