LLevelUp
0
← Back to topic

Arrays

An array stores elements in a single contiguous block of memory. Because every element is the same size and sits next to the previous one, the address of index i is just base + i * elementSize — a constant-time calculation.

Complexity

OperationTime
Access by indexO(1)
Search (unsorted)O(n)
Insert / delete at endO(1) amortized
Insert / delete at frontO(n)

NOTE

“Amortized O(1)” for append means most pushes are cheap, but occasionally the array doubles its capacity and copies everything — that rare O(n) cost spreads out across all the cheap pushes.

A dynamic array

class DynamicArray<T> {
  private data: (T | undefined)[] = new Array(1);
  private size = 0;

  push(value: T): void {
    if (this.size === this.data.length) {
      // Grow by doubling — keeps append amortized O(1).
      const grown = new Array(this.data.length * 2);
      for (let i = 0; i < this.size; i++) grown[i] = this.data[i];
      this.data = grown;
    }
    this.data[this.size++] = value;
  }

  get(index: number): T | undefined {
    return this.data[index];
  }
}

TIP

When you know the final size ahead of time, pre-allocate. You skip every resize-and-copy cycle.