Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exploring a Possible Solution

Course: DSA › Arrays › Introduction

Now that we understand the limitations of using variables and how they prevent us from designing solutions at scale, we can look at the data structure designed to address these problems.


Enter the Array

An array is a contiguous segment of memory that can store multiple data items simultaneously. In its simplest form, an array has a fixed size and can store only a fixed number of data items. All items in an array must be of the same type.

Let’s break that definition down:

  • Contiguous — all elements sit next to each other in memory, no gaps
  • Fixed size — you decide how many items it holds when you create it
  • Same type — you can’t mix integers and strings in the same array

Visually, an array looks like a row of labelled boxes, all the same size, sitting side by side:

---
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"] --- v6["value6"] --- v7["value7"]
    size["◄─────────────── size ───────────────►"] -.- v1

An array data structure.

The size is fixed at creation time. Every cell holds one value of the same type, and every cell is the same width in memory.


Solving the Student Ages Problem

Remember the problem from last lesson — storing ages for an entire class? With separate variables it fell apart at scale. An array solves this cleanly.

Instead of:

ageStudent1 = 12
ageStudent2 = 13
ageStudent3 = 13
... (×108)

You create one array that holds all the ages:

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    a1["age1"] --- a2["age2"] --- a3["age3"] --- a4["age4"] --- a5["age5"] --- a6["age6"] --- a7["age7"]

Storing the ages of students in a class in an array.

One name. One structure. All the values.


Why This Works

Problem with variablesHow arrays fix it
One value per variableOne array holds all values
Hundreds of different namesOne name, access by index
Can’t loop over them easilyLoop with i from 0 to n-1
Not scalableResize once, logic stays the same

Key insight: Instead of naming every value, you name the collection once and refer to items by their position (index).


Key Takeaway

An array is the simplest, most fundamental solution to the “store a collection of same-type values” problem. It trades flexibility (fixed size, fixed type) for speed and simplicity (instant access by index, compact in memory).

Every data structure you’ll learn after this is either built on top of arrays or exists to solve a limitation of arrays.