Array

Arrays

Definition

An array is a linear data structure that stores a collection of elements of the same data type in contiguous memory locations.
Each element is accessed using an index (zero-based in most programming languages).


Characteristics


Advantages


Disadvantages


Operations & Complexity

Operation Time Complexity
Access (by index) O(1)
Search (linear) O(n)
Insert (at end) O(1) / Amortized
Insert (middle) O(n)
Delete (end) O(1)
Delete (middle) O(n)
Look at Big-O Algorithm Complexity Cheat Sheet for more details.

Types of Arrays


Example (in Python)

# Using Python list as a dynamic array
arr = [10, 20, 30, 40, 50]

# Access elements
print("First element:", arr[0])
print("Third element:", arr[2])

# Insert an element
arr.insert(2, 25)
print("After insertion:", arr)

# Delete an element
arr.remove(40)
print("After deletion:", arr)

# Traversal
for i in arr:
    print(i, end=" ")

Use Cases