Arrays vs Other Data Structures: When to Choose Arrays
📖 Scenario: You are working on a simple inventory system for a small bookstore. You need to store the list of book titles currently in stock. You want to decide when to use an array instead of other data structures like objects or sets.
🎯 Goal: Build a TypeScript program that stores book titles in an array, counts how many books are in stock, and prints the list. This will help you understand when arrays are the best choice for storing ordered collections.
📋 What You'll Learn
Create an array called bookTitles with exactly these strings: 'The Hobbit', '1984', 'Brave New World', 'Fahrenheit 451'
Create a variable called totalBooks that stores the number of books in the bookTitles array
Use a for loop with the variable index to iterate over bookTitles
Print each book title on its own line
Print the total number of books after listing all titles
💡 Why This Matters
🌍 Real World
Arrays are used when you need to keep items in order and access them by position, like a list of books on a shelf or songs in a playlist.
💼 Career
Understanding when to use arrays helps in software development jobs where data order matters, such as frontend UI lists, inventory systems, or processing sequences.
Progress0 / 4 steps
1
Create the array of book titles
Create an array called bookTitles with these exact strings in order: 'The Hobbit', '1984', 'Brave New World', 'Fahrenheit 451'
DSA Typescript
Hint
Use square brackets [] to create an array and put the book titles as strings inside, separated by commas.
2
Count the total number of books
Create a variable called totalBooks and set it to the length of the bookTitles array
DSA Typescript
Hint
Use the .length property of the array to get how many items it has.
3
Loop through the array to print each book title
Use a for loop with the variable index to iterate over bookTitles and print each book title using console.log(bookTitles[index])
DSA Typescript
Hint
Start the loop with index = 0 and run it while index is less than bookTitles.length. Use console.log(bookTitles[index]) inside the loop.
4
Print the total number of books
After the loop, print the total number of books using console.log(totalBooks)
DSA Typescript
Hint
Use console.log(totalBooks) after the loop to print the number 4.
Practice
(1/5)
1. Which of the following is the best reason to choose an array over other data structures like linked lists or trees?
easy
A. You need to store key-value pairs.
B. You need fast access to elements by index.
C. You want to insert elements frequently in the middle.
D. You want to maintain a sorted order automatically.
Solution
Step 1: Understand array access speed
Arrays allow direct access to elements using their index in constant time (O(1)).
Step 2: Compare with other structures
Linked lists or trees do not provide constant time index access; they require traversal.
Final Answer:
You need fast access to elements by index. -> Option B
Quick Check:
Fast index access = Array [OK]
Hint: Arrays excel at fast index access, unlike linked lists [OK]
Common Mistakes:
Choosing arrays for frequent middle insertions
Confusing arrays with key-value stores
Assuming arrays keep data sorted automatically
2. Which of the following is the correct way to declare an array of numbers in TypeScript?
easy
A. let arr = {1, 2, 3};
B. let arr: array = (1, 2, 3);
C. let arr: number[] = [1, 2, 3];
D. let arr: number = [1, 2, 3];
Solution
Step 1: Recall TypeScript array syntax
Arrays of numbers are declared as number[] with square brackets after the type.
Step 2: Check each option
let arr: number[] = [1, 2, 3]; uses correct syntax: let arr: number[] = [1, 2, 3];. Others have syntax errors or wrong types.
Accessing arr[2] returns the third element, which is 30.
Step 2: Update array and check second console.log
Setting arr[2] = 50 changes the third element. Printing arr shows [10, 20, 50, 40].
Final Answer:
30 and then [10, 20, 50, 40] -> Option A
Quick Check:
Index access then update = 30 and then [10, 20, 50, 40] [OK]
Hint: Arrays allow element update by index directly [OK]
Common Mistakes:
Thinking arrays are immutable in TypeScript
Confusing index 2 with index 3
Expecting original array after update
4. Identify the error in the following TypeScript code that tries to add an element to an array:
const numbers = [1, 2, 3];
numbers.add(4);
medium
A. The array must be initialized with 'new Array()' to add elements.
B. The array should be declared with 'let' to add elements.
C. The number 4 must be inside square brackets.
D. Arrays don't have an 'add' method; use 'push' instead.
Solution
Step 1: Check method used on array
The method add does not exist for arrays in TypeScript or JavaScript.
Step 2: Identify correct method to add elements
To add elements, use push method like numbers.push(4).
Final Answer:
Arrays don't have an 'add' method; use 'push' instead. -> Option D
Quick Check:
Use push, not add, to add array elements [OK]
Hint: Use push() to add elements to arrays [OK]
Common Mistakes:
Using non-existent 'add' method
Thinking 'let' is required to modify array contents
Trying to add elements with wrong syntax
5. You need to store a large collection of items where you frequently access elements by index and rarely insert or delete items. Which data structure should you choose and why?
hard
A. Array, because it provides fast index-based access and low memory overhead.
B. Linked List, because it allows fast insertions and deletions.
C. Hash Map, because it stores key-value pairs efficiently.
D. Binary Tree, because it keeps data sorted automatically.
Solution
Step 1: Analyze access and modification needs
Frequent index-based access means we want constant time access by position.
Step 2: Compare data structures for this use case
Arrays provide O(1) access by index and use less memory. Insertions/deletions are rare, so array's slower modification is acceptable.
Final Answer:
Array, because it provides fast index-based access and low memory overhead. -> Option A
Quick Check:
Fast index access + rare changes = Array [OK]
Hint: Choose arrays for fast index access and few modifications [OK]
Common Mistakes:
Choosing linked list despite rare insertions
Confusing hash maps with index-based access
Assuming binary trees are best for all data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.