← Back
Data StructureBeginner
📦
Arrays
A row of lockers for your data
⏱️ 3–5 hours🧩 10 LeetCode
🔒 Login to unlock solutions and track progress. Login
What is it?
An array is like a row of lockers numbered from 0.
- Each locker has a number (the index) — so you can jump to any locker instantly.
- You can open any locker quickly if you know its index.
- The items stay in order from left to right.
In programming, arrays are great when you want fast access and simple iteration.
Variants / Subtypes
- Static array — fixed size, memory allocated upfront (e.g. Java
int[]) - Dynamic array — can grow automatically, like JavaScript arrays or Python lists
Real-World Examples
Seats in a cinema row
Student marks list
Daily temperatures
Monthly sales figures
Game scoreboard
Shopping product list
Code Example
What this code shows
Start with [1, 2, 3]. Add 4 to the end and 0 to the front, giving [0,1,2,3,4]. Remove from both ends, leaving [1,2,3]. Check if 2 exists, then print each item.
const arr = [1, 2, 3];
arr.push(4); // add to end → [1, 2, 3, 4]
arr.unshift(0); // add to front → [0, 1, 2, 3, 4]
arr.pop(); // remove last → [0, 1, 2, 3]
arr.shift(); // remove first → [1, 2, 3]
console.log(arr.includes(2)); // check membership → true
// iterate and print each item
for (const x of arr) {
console.log(x); // 1, then 2, then 3
}Expected Output
true 1 2 3