Data Structuresbasics

Arrays: Complete Beginner Guide

TT
Testlaa Team
April 26, 20265 min read

What is an Array?

An array is one of the most fundamental and widely used data structures in programming.

It is used to store multiple values inside a single variable in an ordered manner.

Each value in an array has a position called an index.

Arrays are called linear data structures because elements are stored one after another in sequence.

Python Note

In Python, the structure commonly used like an array is called a list.

Example:

marks = [85, 90, 78]

Technically this is a Python list, but in programming and coding interviews, people often use the word array for this kind of ordered collection.

So in this lesson:

  • Array → general programming concept
  • Python list → Python’s implementation of that concept

This helps us learn the same core ideas used across many programming languages.

Arrays in Different Languages

Different languages use different implementations and names:

Language Structure
C Array
C++ Array / Vector
Java Array / ArrayList
Python List
JavaScript Array
C# Array / List

Even though the names differ, the basic idea is the same:

  • Store multiple values together
  • Access values using indices
  • Maintain the order of elements

Advantages of Arrays

1. Fast random access - O(1)

You can directly access an element using its index.

arr = [10, 20, 30]

print(arr[1])  # 20

The computer can jump straight to the required position without checking previous elements.

2. Efficient sequential storage

Arrays keep elements in order, which makes iteration and sequential processing efficient.

3. Foundation for many data structures

Arrays sit underneath or inspire many important structures and patterns, such as:

  • Stack
  • Queue
  • Hash table
  • Heap
  • Dynamic programming tables
  • Graph representations

Limitations of Arrays

Arrays are very efficient for:

  • Fast indexing
  • Traversal
  • Sequential processing

But operations such as:

  • Inserting in the middle
  • Deleting from the middle

can be slower because elements may need to shift to stay contiguous.


Indexing and Access

Indexing means accessing an element using its position. In Python, indexes usually start at 0 (the first item is arr[0]).

arr = [100, 200, 300]
print(arr[0])  # 100
print(arr[2])  # 300

Step-by-step

  • arr[0] gives 100
  • arr[1] gives 200
  • arr[2] gives 300

Why is it fast?

The computer knows exactly where the element is stored. Accessing any index in an array is O(1). This is known as time complexity.

Invalid index

arr = [1, 2, 3]
# arr[5]

This is like trying to open a locker that does not exist.


Updating values - array_updates

Sometimes we don't just read values - we modify them.

Think of this like opening a locker and replacing the item inside.

Example 1: Basic update

arr = [10, 20, 30]

arr[1] = 99

print(arr)  # [10, 99, 30]

Step-by-step:

  1. Go to index 1
  2. Replace 20 with 99

Only one position is touched → O(1).

Example 2: Increase all values by 5

arr = [10, 20, 30]

for i in range(len(arr)):
    arr[i] = arr[i] + 5

print(arr)  # [15, 25, 35]

Here we update every element → O(n).

Key idea

  • Updating a single indexO(1)
  • Updating the entire arrayO(n)

Processing arrays - array_processing

Processing means going through the array and doing some work on each element.

Example 1: Find sum

arr = [10, 20, 30]

total = 0

for num in arr:
    total += num

print(total)  # 60

Example 2: Count even numbers

arr = [1, 2, 3, 4, 5, 6]

count = 0

for num in arr:
    if num % 2 == 0:
        count += 1

print(count)  # 3

Example 3: Create a new array (double values)

arr = [1, 2, 3]

result = []

for num in arr:
    result.append(num * 2)

print(result)  # [2, 4, 6]

Key idea

You visit each element → O(n). This is one of the most common patterns in coding problems.


Comparing elements - array_comparison

Comparison is used when we need to decide something between elements - e.g. find largest/smallest, compare neighbors, check order.

Example 1: Find maximum

arr = [5, 2, 9, 1]

max_val = arr[0]

for num in arr:
    if num > max_val:
        max_val = num

print(max_val)  # 9

Example 2: Find minimum

arr = [5, 2, 9, 1]

min_val = arr[0]

for num in arr:
    if num < min_val:
        min_val = num

print(min_val)  # 1

Example 3: Check if array is sorted

arr = [1, 2, 3, 4]

is_sorted = True

for i in range(len(arr) - 1):
    if arr[i] > arr[i + 1]:
        is_sorted = False

print(is_sorted)  # True

Key idea

Comparisons usually happen inside a loop over the arrayO(n) in these patterns.


Deleting elements - array_deletion

Deleting from an array often costs more than overwriting a slot, because many array implementations keep elements contiguous in memory.

Example 1: Delete at index (pop)

arr = [10, 20, 30, 40]

arr.pop(1)

print(arr)  # [10, 30, 40]

What effectively happens:

Before: [10, 20, 30, 40]
Index:    0   1   2   3

Delete index 1

After shifting: [10, 30, 40]

Remaining elements shift left to fill the gap.

Example 2: Remove first occurrence of a value

arr = [10, 20, 30, 20]

arr.remove(20)

print(arr)  # [10, 30, 20]

First occurrence only.

Example 3: Drop all occurrences of a value (new list)

arr = [10, 20, 30, 20]

result = []

for num in arr:
    if num != 20:
        result.append(num)

print(result)  # [10, 30]

Key idea

Deleting or removing from the middle (and keeping one contiguous block) tends to involve shiftingO(n) for that operation in the typical dynamic array/list model.


Key Takeaways

  • Index gives instant access
  • Arrays are the foundation for many patterns
  • You must understand indexing before advanced problems

Tags:

ArraysBasicsData StructuresCodingAlgorithms