Overview of Supported Operations
Course: DSA › Arrays › Introduction
Now that we know the logical representation of an array, let’s examine how to create, access, modify, and traverse one. Almost all major programming languages support arrays in some form.
Creating an Array
The syntax and rules for creating an array depend on the programming language. An array with a fixed size cannot be modified after creation, and all data items in an array must be of the same type.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
v1["value1"] --- v2["value2"] --- v3["value3"] --- v4["value4"] --- v5["value5"]
Creating an array of fixed size and datatype.
Higher-level languages like Python inherently provide a list instead of a raw array. A list behaves like an array but has a dynamic size and can store elements of different types. However, the underlying machine-level implementation still uses basic arrays as the core data structure, which has a fixed size and type.
from typing import List
# Python lists are dynamic and can grow or shrink at runtime
# Declaring an array (list) of fixed size with default values
numbers: List[int] = [0] * 5
# Declaring and initializing an array
numbers2: List[int] = [1, 2, 3, 4, 5]
# Creating an array of size N
size_n: int = 5
numbers3: List[int] = [0] * size_n
# Creating and initializing using list comprehension
numbers4: List[int] = [i for i in range(5)]
Tip: In Python, annotating with
List[int]is just a hint — the runtime won’t enforce it. But it’s good practice to document your intent, especially for DSA problems.
Accessing Elements in an Array
An array is a collection of data items stored in contiguous memory. This layout allows us to access any element directly using its index via the subscript operator [].
Why do array indices start from 0 instead of 1?
Array indices represent an element’s relative position from the array’s beginning. The first element is 0 steps away from the start, the second is 1 step away, and so on. This is not a convention — it’s a direct reflection of how address arithmetic works in memory.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
v1["value1<br/>─────<br/>0"] --- v2["value2<br/>─────<br/>1"] --- v3["value3<br/>─────<br/>2"] --- v4["value4<br/>─────<br/>3"] --- v5["value5<br/>─────<br/>4"]
Array elements are accessed via their indices.
Different languages have different syntax, but the underlying access mechanism is the same for all.
from typing import List
# Initializing an array (list)
numbers: List[int] = [1, 2, 3, 4, 5]
# Accessing elements using the subscript [] operator
print("1st value:", numbers[0]) # → 1
print("5th value:", numbers[4]) # → 5
# Negative indexing (Python-specific convenience)
print("Last value:", numbers[-1]) # → 5
Common mistake: Accessing
numbers[5]in a 5-element array raises anIndexError. Valid indices are0tolen(numbers) - 1.
Modifying Elements in an Array
Elements in an array can be modified in place, just like variables. To update a value, use array[index] on the left side of the assignment operator.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
v1["value1<br/>─────<br/>0"] --- v2["value2<br/>─────<br/>1"] --- v3["value3<br/>─────<br/>2"] --- v4["value4<br/>─────<br/>3"] --- v5["value5<br/>─────<br/>4"]
style v2 fill:#fde68a,stroke:#d97706,color:#1a1a1a
style v3 fill:#fde68a,stroke:#d97706,color:#1a1a1a
Array elements can be modified via their indices (highlighted = being updated).
from typing import List
# Initializing an array
numbers: List[int] = [1, 2, 3, 4, 5]
# Modifying array elements using the subscript [] operator
numbers[0] = 10
numbers[2] = 30
numbers[4] = 50
# Printing modified values
print("1st value:", numbers[0]) # → 10
print("3rd value:", numbers[2]) # → 30
print("5th value:", numbers[4]) # → 50
Different languages implement this differently at the syntax level, but the underlying mechanism — overwriting a memory location at a known address — is the same everywhere.
Traversing an Array
Traversal is one of the most common operations on an array. It is the only way to search for a value in an array and is implemented using a loop control variable as an index, starting from 0. To traverse safely, the size of the array must be known.
The pointer starts at index 0 and steps forward one cell at a time until it reaches the end:
index = 0
Traversing an array using a loop control variable index. Click Next/Prev to step through.
Higher-level languages have built-in functions to get the array’s length. For lower-level languages like C/C++, the programmer needs to track the array’s size manually.
from typing import List
# Initializing an array (list)
numbers: List[int] = [1, 2, 3, 4, 5]
# 1. Traversal using index-based for loop
for index in range(len(numbers)):
print(numbers[index])
# 2. Traversal using direct for-each loop
for value in numbers:
print(value)
# 3. Traversal using enumerate (index + value)
for index, value in enumerate(numbers):
print(index, value)
# 4. Traversal using while loop
index: int = 0
while index < len(numbers):
print(numbers[index])
index += 1
Which to use?
- Use
for value in numberswhen you only need the value- Use
for index, value in enumerate(numbers)when you need both- Use
whilewhen you need finer control (e.g. skip indices, step by 2)
Summary
| Operation | Syntax | Time Complexity |
|---|---|---|
| Create | numbers = [0] * n | O(n) |
| Access | numbers[i] | O(1) |
| Modify | numbers[i] = x | O(1) |
| Traverse | for i in range(len(numbers)) | O(n) |
Access and modify are O(1) because the CPU computes the exact memory address directly from the index — no searching required. Traversal is O(n) because every element must be visited.