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

Understanding the Problem

Course: DSA › Arrays › Introduction

To understand arrays and why we need them, let’s look at a real problem that programmers run into all the time when designing software systems.

When writing a program, we often need to store a collection of related data items that can be accessed sequentially. For example, imagine storing the ages of all the students in a class.

If there are only a few students, storing them in separate variables feels fine:

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    A["  ageStudent1 = 12  "] ~~~ B["  ageStudent2 = 13  "] ~~~ C["  ageStudent3 = 13  "]

Using variables to store the ages of 3 students.

Easy enough. Three students, three variables. Done.

But what happens when the class has hundreds of students? Now you need hundreds of variables:

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    s1["  ageStudent1 = 12  "] ~~~ s2["  ageStudent2 = 13  "] ~~~ s3["  ageStudent3 = 13  "] ~~~ s4["  ageStudent4 = 11  "]
    s5["  ageStudent5 = 11  "] ~~~ s6["  ageStudent6 = 12  "] ~~~ s7["  ageStudent7 = 12  "] ~~~ s8["  ageStudent8 = 13  "]
    s9["  ......  "] ~~~ s10["  ......  "] ~~~ s11["  ......  "] ~~~ s12["  ......  "]
    s13["  ageStudent105 = 13  "] ~~~ s14["  ageStudent106 = 11  "] ~~~ s15["  ageStudent107 = 13  "] ~~~ s16["  ageStudent108 = 11  "]

Using variables to store ages of 108 students.

While this technically works, storing and managing hundreds of values across hundreds of individually named variables is error-prone and not scalable.


Limitations of Using Variables

Variables are incredibly useful for holding individual pieces of data. But when you try to use them to store a collection of related data, they start to break down fast.

Here’s why:

  • A variable can store only one value at a time.
  • Different variables that store the same type of information must have different names. You can’t call two things ageStudent — one has to be ageStudent1, another ageStudent2, and so on forever.
  • Too many variables complicate the source code and the programming logic, making it error-prone.
  • Using variables to store lots of data is not scalable. What happens when the class size changes? You’d need to add or remove variable declarations manually.

The real problem: Variables are designed for individual values. They were never meant to handle collections.


Why This Matters

Computers are designed to solve problems at scale — managing large amounts of data that would be impossible for humans to handle manually. Problems like these (storing and processing collections) are extremely common, even in the simplest software.

That’s why even the lowest-level programming languages, such as assembly, inherently support a data structure for storing multiple values together.

That data structure is what we’re about to learn: the array.


Key Takeaway

ApproachWorks forBreaks when
Separate variables2–3 valuesData grows, or you need to loop over it
ArraysAny number of values(Rarely — this is exactly what they’re built for)

The moment you find yourself typing variable1, variable2, variable3… stop. You need an array.