Array Basics

data structures algorithms programming fundamentals software engineering computer science
An introduction to arrays as a fundamental data structure: what they are, how they store elements, how to access and modify them, common operations and their performance, multidimensional arrays, and best practices.

What Is an Array?

An array is a collection of elements of the same type, stored in a contiguous block of memory and accessible by an index. Arrays provide fast random access to elements and are one of the most widely used data structures in computing.

Why Arrays Matter

  • Performance: Constant-time (O(1)) random access by index.
  • Predictable layout: Contiguous memory improves cache locality.
  • Foundation: Many structures (strings, matrices, dynamic arrays, hash tables, heaps) build on arrays.

Core Properties

  • Homogeneous elements: All elements share the same type (or size) in most languages.
  • Indexing: Typically zero-based (0..n-1), though some languages use one-based indexing (1..n).
  • Size: Fixed-length arrays have a size determined at creation; dynamic arrays (array lists) can grow/shrink by resizing under the hood.
  • Contiguity: Elements are stored back-to-back in memory, enabling pointer arithmetic and fast traversal.

Creating and Accessing

// Pseudocode
let a = new Array of length 5
// Initialize
for i from 0 to 4:
  a[i] = 0

// Access and update
x = a[2]        // read
a[2] = x + 10   // write

Accessing or updating an element by index is O(1). Accessing outside the valid range (e.g., a[-1] or a[n]) is an out-of-bounds error.

Iteration Patterns

// Index-based loop
for i from 0 to n-1:
  visit(a[i])

// Enhanced/for-each (conceptual)
for value in a:
  visit(value)

Sequential iteration is cache-friendly and typically very fast.

Common Operations and Complexity

  • Read/Write by index: O(1)
  • Search (unsorted): O(n) linear scan
  • Search (sorted with binary search): O(log n)
  • Insert/Delete at end (dynamic array): Amortized O(1)
  • Insert/Delete at start or middle: O(n), due to shifting elements
  • Resize (dynamic arrays): Occasional O(n) copy when capacity grows
  • Slicing: Often O(k) to copy k elements; some systems provide O(1) “views” that reference the same data

Fixed vs. Dynamic Arrays

  • Fixed-length arrays: Size set at creation; memory is a contiguous block. Efficient but inflexible.
  • Dynamic arrays: Backed by a fixed array that resizes (typically doubling capacity) when full. Append is amortized O(1); occasional O(n) resize.

Multidimensional Arrays

  • True multidimensional (contiguous): A 2D array stored in row-major (rows contiguous) or column-major (columns contiguous) order.
  • Array of arrays (jagged): Each row is its own array; rows can have different lengths.
// 2D indexing (row, col)
value = m[r][c]

// Traversal (row-major friendly)
for r from 0 to R-1:
  for c from 0 to C-1:
    visit(m[r][c])

For performance-sensitive code, align traversal with memory layout (row-major vs. column-major) to maximize cache locality.

Copying, Aliasing, and Mutability

  • Shallow copy: Copies references to elements (or the top-level container) without duplicating nested data.
  • Deep copy: Recursively duplicates all nested structures.
  • Aliasing: Two variables can refer to the same array; changes through one name are visible through the other.
// Aliasing example
b = a       // b and a refer to the same array
b[0] = 42   // a[0] is now 42 as well

Common Pitfalls

  • Off-by-one errors: Remember valid indices are typically 0..n-1.
  • Out-of-bounds access: Can throw errors or cause undefined behavior.
  • Incorrect assumptions about copying: Know whether operations create views or copies.
  • Costly mid-array insertions/deletions: Prefer data structures optimized for these operations when needed.

Practical Examples

// Sum all elements
sum = 0
for i from 0 to n-1:
  sum += a[i]

// Reverse in-place
left = 0
right = n - 1
while left < right:
  swap(a[left], a[right])
  left += 1
  right -= 1

// Remove consecutive duplicates (in-place, assuming sorted)
if n > 0:
  write = 1
  for read from 1 to n-1:
    if a[read] != a[read - 1]:
      a[write] = a[read]
      write += 1
  // first 'write' elements are unique

Use Cases

  • Buffers and streams: Network packets, I/O buffers.
  • Media data: Pixels in images, samples in audio.
  • Numerical computing: Vectors, matrices, tensors.
  • Lookup tables: Direct indexing for small key ranges.

Choosing Arrays vs. Other Structures

  • Use arrays when you need fast random access and contiguous storage.
  • Consider linked lists for frequent insertions/deletions in the middle (with trade-offs).
  • Use dynamic arrays for flexible growth with mostly end-appends.
  • Use hash tables or trees for fast keyed lookup not based on indices.

Key Takeaways

  • Arrays provide O(1) index-based access and excellent cache performance.
  • Mid-array insertions/deletions are O(n) due to shifting.
  • Understand your environment's indexing, bounds behavior, and copy semantics.
  • Leverage arrays as building blocks for higher-level data structures and algorithms.

Context from Referenced By

Context from Related Topics
Pop Quiz
Topic: array_basics
Level: 1
True or False:

In a dynamic array, appending an element has amortized O(1) time complexity because occasional resizes copy elements only when capacity is exhausted.

Topic: array_basics
Level: 2
Multiple Choice:

In a dynamic array that resizes by doubling when full, what is the time complexity of inserting a new element at the beginning (index 0)?

Topic: array_basics
Level: 1
Multiple Choice:

In a 2D array stored in row-major order, which traversal is most cache-friendly when visiting every element?

Topic: array_basics
Level: 2
Fill in the Blank:

Arrays store elements in a _____ block of memory, enabling fast random access by index.

Topic: array_basics
Level: 1
Fill in the Blank:

In most languages, an array stores its elements in _____ memory, enabling O(1) access by index and good cache locality.

Topic: array_basics
Level: 2
True or False:

In a jagged (array-of-arrays) 2D structure, different rows can have different lengths because each row is a separate array.

Topic: array_basics
Level: 3
True or False:

In a jagged 2D array (array of arrays), different rows can have different lengths.

Topic: array_basics
Level: 2
Multiple Choice:

In a language where arrays are mutable reference types, what happens after executing b = a followed by b[0] = 42?

Topic: array_basics
Level: 3
Multiple Choice:

In a system where array slicing returns an O(1) view that references the original data, what happens if you modify an element through the slice?

Topic: array_basics
Level: 2
Fill in the Blank:

A shallow copy of an array of objects duplicates the _____ but not the underlying objects.

Topic: array_basics
Level: 3
Fill in the Blank:

Compared to a deep copy, a shallow copy of an array of objects duplicates only the _____, not the nested data.

Topic: array_basics
Level: 3
True or False:

Fixed-length arrays have a size that is set at creation and cannot be changed without allocating a new array.

Topic: array_basics
Level: 4
True or False:

Binary search on a sorted array runs in O(log n) time.

Topic: array_basics
Level: 3
Multiple Choice:

In a 2D array stored in column-major order, which traversal is most cache-friendly when visiting every element?

Topic: array_basics
Level: 3
Fill in the Blank:

In a zero-based array of length n, the last valid index is _____.

Topic: array_basics
Level: 4
Multiple Choice:

In a language where array slicing copies elements into a new array, what is the time complexity of taking a slice of length k, and how do subsequent modifications to the slice affect the original array?

Topic: array_basics
Level: 4
True or False:

In a zero-based array of length n, accessing a[-1] is an out-of-bounds error.

Topic: array_basics
Level: 4
Fill in the Blank:

When a dynamic array runs out of space, it typically resizes by _____ its capacity to keep appends amortized O(1).

Topic: array_basics
Level: 4
Multiple Choice:

In the provided in-place algorithm for removing consecutive duplicates from a sorted array, what are the worst-case time and additional space complexities?

Topic: array_basics
Level: 5
True or False:

In a jagged (array-of-arrays) 2D structure, elements at the same column index across different rows are guaranteed to be stored contiguously in memory.

Topic: array_basics
Level: 4
Fill in the Blank:

In a dynamic array, inserting or deleting in the middle is O(n) due to _____ elements.

Topic: array_basics
Level: 5
Multiple Choice:

When appending n elements one by one to an initially empty dynamic array that doubles its capacity when full, what is the total time complexity of all appends combined?

Topic: array_basics
Level: 5
True or False:

All programming languages use zero-based array indexing.

Topic: array_basics
Level: 5
Fill in the Blank:

Searching for a value in an unsorted array typically takes _____ time.

Topic: array_basics
Level: 5
Multiple Choice:

For an array of length n, what are the time and additional space complexities of reversing it in place using the two-pointer swap method?

Topic: array_basics
Level: 5
Fill in the Blank:

Sequential iteration over a contiguous array is typically _____ due to spatial locality.

Next Topic
used_by
0.92

Dynamic Array
Dynamic arrays are implemented atop raw arrays, relying on array indexing and contiguous storage while adding resizing strategies for amortized constant-time append.
used_by
0.9

Search Algorithms
Search algorithms like linear and binary search operate over arrays, relying on indexing, bounds, and iteration; understanding arrays underpins their implementation and complexity analysis.
used_by
0.9

Stacks And Queues
Stacks and queues are commonly implemented atop arrays (e.g., using a top index for stacks and circular buffers for queues), so understanding array indexing, capacity, and resizing leads directly into their design and performance.
used_by
0.9

Dynamic Arrays
Dynamic arrays (e.g., ArrayList, Vector) build on contiguous arrays, adding resizing strategies and amortized insertion while preserving index-based access.
used_by
0.88

Dynamic Arrays
Dynamic arrays build on fixed-size array semantics to provide automatic resizing, amortized append, and flexible capacity management.
used_by
0.88

Sorting Algorithms
Sorting algorithms act on indexable sequences; knowledge of array indexing, contiguity, and in-place mutation enables implementing and analyzing common sorts.
used_by
0.88

Sorting Algorithms
Sorting algorithms operate on arrays, relying on indexing, traversal, comparisons, and in-place element swaps introduced in array basics.
used_by
0.88

Hash Tables
Hash tables use arrays for bucket storage; understanding indexing, bucket arrays for collision handling, and resizing strategies naturally follows from array fundamentals.
used_by
0.88

Sorting Algorithms
Mastery of array properties like contiguous memory, indexing, and in-place updates directly informs how sorting algorithms are implemented and analyzed.
used_by
0.86

Sorting Algorithms
Understanding arrays (indexing, iteration, contiguous memory, and in-place updates) is foundational for implementing and reasoning about common sorting techniques like bubble, insertion, selection, quicksort, and mergesort.
used_by
0.86

Sorting Algorithms
Understanding arrays (contiguous storage, indexing, in-place updates) is essential for implementing and reasoning about common sorting methods that operate on indexable sequences.
used_by
0.82

Matrix Operations
Matrix operations (addition, multiplication, transpose) are implemented over multidimensional arrays and rely on correct indexing, iteration, and memory layout understanding.