Beginner Topic 2 of 52 Data Structures
Dynamic Arrays
Resizable arrays that grow by reallocating with amortized cost.
How it works
A dynamic array wraps a fixed array with a size and a capacity. When the capacity is full, it allocates a larger block, typically double, copies the elements over and frees the old block. Because the cost of each copy is spread across many appends, appending takes amortized O(1) time even though a single append can occasionally take O(n).
Step-by-step walkthrough
- The array has capacity 8 and holds 4 items.
- Appending the 5th item uses spare capacity in constant time.
- Items 6 to 8 fill the remaining slots.
- When it is full, a new array of double size is allocated and everything is copied.
- Copies are rare, so append is amortized O(1).
Where it is used
Python lists, Java ArrayList, C++ vector and JavaScript arrays are dynamic arrays. They are the standard sequence type for collecting results, building output, buffering input and implementing stacks.
Common pitfall
Resizing invalidates pointers and iterators into the old block. Deleting from the middle or front is still O(n), and over-allocation wastes memory. Reserve capacity when the final size is known to avoid repeated reallocations, and shrink only when memory is tight.
Where it fits in the course
Dynamic Arrays is topic 2 of 52 in Data Structures, rated Beginner. It follows Arrays. The next topic is Linked Lists.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Dynamic Arrays?
Where do people use Dynamic Arrays?
What is the most common mistake with Dynamic Arrays?
What should I learn before this topic?
How can I practice it?
Is Dynamic Arrays hard to learn?
How long does it take to learn Dynamic Arrays?
Which topics are related to Dynamic Arrays?
What should I study after Dynamic Arrays?
Related topics
- Arrays (Data Structures)
- Linked Lists (Data Structures)
- Doubly Linked Lists (Data Structures)
- Circular Linked Lists (Data Structures)
- Static vs Dynamic Typing (Languages & Compilers)
- Arrays and Lists (Programming Fundamentals)
- Dynamic Programming (Algorithms)