← Back to DSA Course | Chapter 2: Arrays | Lesson 2 of 8

Array Traversal and Operations

Traversal

Traversal means visiting every element of an array exactly once, almost always with a for loop that walks from index 0 to size−1. It's the foundation nearly every other array operation is built on top of.

Example: Traversal

#include <iostream>
using namespace std;
int main() {
    int arr[] = {3, 6, 9, 12};
    for (int i = 0; i < 4; i++) cout << arr[i] << " "; // visit each element once
    cout << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] arr = {3, 6, 9, 12};
        for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " ");
        System.out.println();
    }
}
arr = [3, 6, 9, 12]
for x in arr:
    print(x, end=" ")
print()
#include <stdio.h>
int main() {
    int arr[] = {3, 6, 9, 12};
    for (int i = 0; i < 4; i++) printf("%d ", arr[i]);
    printf("\n");
    return 0;
}

Searching

Linear search traverses the array and compares each element to a target value, stopping early if it finds a match. It's simple and works on unsorted data, though it takes O(n) time in the worst case since it may have to check every element.

Example: Searching

#include <iostream>
using namespace std;
int main() {
    int arr[] = {9, 4, 7, 2, 5};
    int target = 7, foundAt = -1;
    for (int i = 0; i < 5; i++) {
        if (arr[i] == target) { foundAt = i; break; } // stop early on match
    }
    cout << "Found at index: " << foundAt << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] arr = {9, 4, 7, 2, 5};
        int target = 7, foundAt = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) { foundAt = i; break; }
        }
        System.out.println("Found at index: " + foundAt);
    }
}
arr = [9, 4, 7, 2, 5]
target = 7
found_at = arr.index(target) if target in arr else -1
print("Found at index:", found_at)
#include <stdio.h>
int main() {
    int arr[] = {9, 4, 7, 2, 5};
    int target = 7, foundAt = -1;
    for (int i = 0; i < 5; i++) {
        if (arr[i] == target) { foundAt = i; break; }
    }
    printf("Found at index: %d\n", foundAt);
    return 0;
}

Insertion and Deletion

Because a fixed-size array stores elements contiguously, inserting or deleting in the middle means physically shifting every element after that position over by one slot, which is an O(n) operation, unlike a linked list where the same change is O(1) once you're at the right node.

Example: Insertion and Deletion

#include <iostream>
using namespace std;
int main() {
    int arr[6] = {1, 2, 4, 5, 0, 0};
    int n = 4;
    // Insert 3 at index 2: shift everything after it right by one.
    for (int i = n; i > 2; i--) arr[i] = arr[i - 1];
    arr[2] = 3;
    n++;
    for (int i = 0; i < n; i++) cout << arr[i] << " ";
    cout << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 5, 0, 0};
        int n = 4;
        for (int i = n; i > 2; i--) arr[i] = arr[i - 1];
        arr[2] = 3;
        n++;
        for (int i = 0; i < n; i++) System.out.print(arr[i] + " ");
        System.out.println();
    }
}
arr = [1, 2, 4, 5]
arr.insert(2, 3)  # shifting every later element right by one, O(n)
print(*arr)
#include <stdio.h>
int main() {
    int arr[6] = {1, 2, 4, 5, 0, 0};
    int n = 4;
    for (int i = n; i > 2; i--) arr[i] = arr[i - 1];
    arr[2] = 3;
    n++;
    for (int i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");
    return 0;
}

Counting and Frequency

Counting how many times a value appears, or building a full frequency count of every distinct value, is a traversal that keeps a running tally as it scans, often using a separate array or hash map indexed by value.

Example: Counting and Frequency

#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 2, 2, 3, 2, 1};
    int target = 2, count = 0;
    for (int i = 0; i < 6; i++)
        if (arr[i] == target) count++;
    cout << "Count of " << target << ": " << count << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 2, 1};
        int target = 2, count = 0;
        for (int x : arr) if (x == target) count++;
        System.out.println("Count of " + target + ": " + count);
    }
}
arr = [1, 2, 2, 3, 2, 1]
target = 2
count = arr.count(target)
print(f"Count of {target}:", count)
#include <stdio.h>
int main() {
    int arr[] = {1, 2, 2, 3, 2, 1};
    int target = 2, count = 0;
    for (int i = 0; i < 6; i++)
        if (arr[i] == target) count++;
    printf("Count of %d: %d\n", target, count);
    return 0;
}

Useful Array Operations

Practicing sum, average, and reversal on arrays builds real intuition for index arithmetic and loop boundaries. These simple operations are also the building blocks for more advanced array techniques like two-pointer and sliding-window algorithms.

Example: Useful Array Operations

#include <iostream>
using namespace std;
int main() {
    int arr[] = {4, 8, 15, 16, 23};
    int n = 5, sum = 0;
    for (int i = 0; i < n; i++) sum += arr[i];
    double avg = (double)sum / n;
    for (int i = 0; i < n / 2; i++) swap(arr[i], arr[n - 1 - i]); // reversal
    cout << "Sum: " << sum << ", Avg: " << avg << ", arr[0] after reverse: " << arr[0] << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] arr = {4, 8, 15, 16, 23};
        int n = arr.length, sum = 0;
        for (int x : arr) sum += x;
        double avg = (double) sum / n;
        for (int i = 0; i < n / 2; i++) {
            int tmp = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = tmp;
        }
        System.out.println("Sum: " + sum + ", Avg: " + avg + ", arr[0] after reverse: " + arr[0]);
    }
}
arr = [4, 8, 15, 16, 23]
total = sum(arr)
avg = total / len(arr)
arr.reverse()
print("Sum:", total, ", Avg:", avg, ", arr[0] after reverse:", arr[0])
#include <stdio.h>
int main() {
    int arr[] = {4, 8, 15, 16, 23};
    int n = 5, sum = 0;
    for (int i = 0; i < n; i++) sum += arr[i];
    double avg = (double)sum / n;
    for (int i = 0; i < n / 2; i++) {
        int tmp = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = tmp;
    }
    printf("Sum: %d, Avg: %.1f, arr[0] after reverse: %d\n", sum, avg, arr[0]);
    return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.