Chapter 06 of 32

Arrays in DSA

One of the first and most important data structures you'll learn in DSA is the array.

If you have a group of values of the same type and want to store them together, an array is one of the simplest options.

For example, instead of creating separate variables:

int mark1 = 85;
int mark2 = 92;
int mark3 = 78;
int mark4 = 90;

we can use an array:

int[] marks = {85, 92, 78, 90};

Now all the marks are stored together.

What is an Array?

An array is a data structure that stores multiple values of the same type in a fixed-size sequence.

For example:

int[] numbers = {10, 20, 30, 40, 50};

Here, we have an array containing five integers.

You can visualize it like this:

Index:   0    1    2    3    4
         ↓    ↓    ↓    ↓    ↓
       ┌────┬────┬────┬────┬────┐
       │ 10 │ 20 │ 30 │ 40 │ 50 │
       └────┴────┴────┴────┴────┘

Each value has a position called an index.

And remember, array indexing starts from 0, not 1.

So:

marks[0] → 85
marks[1] → 92
marks[2] → 78
marks[3] → 90

Creating an Array

There are several ways to create an array in Java.

We can create and initialize it at the same time:

int[] numbers = {10, 20, 30, 40, 50};

Or we can create an array with a specific size:

int[] numbers = new int[5];

This creates space for five integers.

Initially, the elements contain Java's default value for int, which is 0.

0
0
0
0
0

We can then assign values:

numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

Accessing Elements

We can access an element using its index.

int[] numbers = {10, 20, 30, 40, 50};

System.out.println(numbers[2]);

Output:

30

Why?

Because index 2 contains 30.

Index:   0    1    2    3    4
Value:  10   20   30   40   50
                  ↑
                30

Changing an Element

Array elements can be changed using their index.

int[] numbers = {10, 20, 30, 40, 50};

numbers[2] = 100;

Now the array becomes:

10, 20, 100, 40, 50

We simply replaced the value at index 2.

Array Length

We can find the number of elements in an array using the length property.

int[] numbers = {10, 20, 30, 40, 50};

System.out.println(numbers.length);

Output:

5

Notice that for arrays, we use:

numbers.length

not:

numbers.length()

length is a property, not a method.

Traversing an Array

One of the most common things we do with an array is traverse it, which simply means visiting each element.

We can use a normal for loop:

int[] numbers = {10, 20, 30, 40, 50};

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

Output:

10
20
30
40
50

Here, i represents the index.

We can also use an enhanced for loop:

for (int number : numbers) {
    System.out.println(number);
}

This is simpler when we only need the values and don't need the indexes.

Finding an Element

Suppose we want to find whether 30 exists in an array.

We can use linear search:

int[] numbers = {10, 20, 30, 40, 50};

int target = 30;

for (int i = 0; i < numbers.length; i++) {

    if (numbers[i] == target) {
        System.out.println("Found at index " + i);
        break;
    }
}

Output:

Found at index 2

In the worst case, we may have to check every element.

So the time complexity is:

O(n)

Finding the Largest Element

Arrays are also commonly used in problems where we need to find the maximum or minimum value.

For example:

int[] numbers = {10, 25, 7, 40, 18};

int largest = numbers[0];

for (int i = 1; i < numbers.length; i++) {

    if (numbers[i] > largest) {
        largest = numbers[i];
    }
}

System.out.println("Largest: " + largest);

Output:

Largest: 40

We start by assuming the first element is the largest.

Then we compare it with every other element and update largest whenever we find a bigger value.

This takes O(n) time because we look through the array once.

Inserting Elements

Arrays have a fixed size in Java.

For example:

int[] numbers = new int[5];

This array can hold exactly five integers.

You can't simply add a sixth element like you can with an ArrayList.

If you need a larger array, you generally have to create a new one and copy the elements.

This fixed size is one of the main differences between arrays and dynamic data structures such as ArrayList.

Deleting Elements

Arrays also don't have a built-in operation that physically removes an element and automatically reduces the array size.

For example, suppose we have:

10, 20, 30, 40, 50

If we want to "delete" 30, we could shift the elements:

10, 20, 40, 50

But the original array still has the same fixed capacity.

This is one reason other data structures can be more convenient when frequent insertion and deletion are required.

Array Operations and Their Complexity

Let's look at some common operations.

Operation

Typical Time

Access by index

O(1)

Update by index

O(1)

Linear search

O(n)

Traverse

O(n)

Find minimum/maximum

O(n)

Accessing an element by index is particularly fast.

For example:

numbers[3]

Java can directly access the element at index 3, so we consider this:

O(1)

It doesn't need to start from the first element and move toward index 3.

Why Is Array Access O(1)?

Imagine the array as a row of numbered lockers.

If someone tells you:

"Give me the item in locker number 50."

You can directly go to locker 50.

You don't need to open lockers 0 through 49 first.

That's similar to how array indexing works.

So:

numbers[50]

is an O(1) operation.

Arrays and Memory

Arrays store elements in a structured sequence in memory.

This is one reason arrays can provide fast index-based access.

For example:

┌────┬────┬────┬────┬────┐
│ 10 │ 20 │ 30 │ 40 │ 50 │
└────┴────┴────┴────┴────┘
  0    1    2    3    4

The elements are stored as part of the same array structure, allowing Java to calculate where a particular index is located.

You don't need to understand the exact memory addresses yet. The important idea is that arrays are designed for fast access using indexes.

A Real-Life Example

Imagine a cinema has 100 numbered seats:

Seat 1
Seat 2
Seat 3
...
Seat 100

Each seat has a fixed position.

If someone tells you:

"Check seat number 75."

You can directly go to seat 75.

An array works similarly. Each element has a position, and we can access it directly using its index.

A Complete Example

Let's write a small program that calculates the average marks of a student:

class Main {
    public static void main(String[] args) {

        int[] marks = {85, 92, 78, 90, 88};

        int sum = 0;

        for (int mark : marks) {
            sum += mark;
        }

        double average = (double) sum / marks.length;

        System.out.println("Average marks: " + average);
    }
}

Output:

Average marks: 86.6

Here, the array stores the marks, and a loop processes each element to calculate the total.

When Should You Use an Array?

Arrays are a good choice when:

  • You know the number of elements in advance.

  • You need fast access using an index.

  • You want to store elements of the same type.

  • You don't need frequent resizing.

  • You want a simple and efficient structure for sequential data.

If the number of elements can change frequently, a dynamic collection such as ArrayList may be more convenient.

The main thing to remember is:

An array is a fixed-size data structure that stores elements of the same type and provides fast access using indexes.

Arrays are one of the foundations of DSA. Once you're comfortable with arrays, many important DSA concepts such as searching, sorting, two pointers, sliding window, prefix sums, and many other techniques become much easier to understand.