DSA Arrays
Learn how arrays store ordered values and support fast access through indexes.
Overview
Arrays are one of the most important data structures in programming. They appear in everything from calculators to social platforms and games. Because many other structures and algorithms build on the same ideas, arrays are an ideal starting point for learning DSA.
Key concepts
- An array stores multiple related values under one variable name
- Every stored value is an element with a unique index
- Indexes usually begin at zero
- Knowing an index provides direct O(1) access to an element
What is an Array?
An array is an ordered collection of values stored under a single variable name. Think of it as a row of numbered lockers: each locker holds one item, and its number tells you where to find it.
Index: 0 1 2 3 4
Array: [10, 20, 30, 40, 50]In this array, 10 is the first element at index 0, while 50 is the last element at index 4. Most programming languages use zero-based indexing.
Why do we use Arrays?
Without an array, every related value would need its own variable.
let score1 = 85;
let score2 = 90;
let score3 = 78;
let score4 = 95;An array keeps the same values together, making the code cleaner and easier to manage.
const scores = [85, 90, 78, 95];Creating an Array
JavaScript arrays can contain numbers, strings, booleans, objects, or even other arrays.
const fruits = ["Apple", "Banana", "Orange", "Mango"];
console.log(fruits);
// ["Apple", "Banana", "Orange", "Mango"]Accessing elements
Place an index inside square brackets to access the element at that position.
const fruits = ["Apple", "Banana", "Orange", "Mango"];
console.log(fruits[0]); // Apple
console.log(fruits[2]); // OrangeUpdating an element
Assigning a new value at an existing index updates that element. This is fast because the index directly identifies its position.
const fruits = ["Apple", "Banana", "Orange"];
fruits[1] = "Grapes";
console.log(fruits);
// ["Apple", "Grapes", "Orange"]Traversing an Array
Traversing means visiting every element one by one. It is useful for calculating totals, searching for values, or updating multiple elements.
const numbers = [10, 20, 30, 40];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}Common Array operations
- Access an element
- Update an element
- Insert a new element
- Delete an element
- Traverse all elements
- Search for a value
- Sort elements
Time complexity of common operations
| Operation | Time complexity |
|---|---|
| Access by index | O(1) |
| Update by index | O(1) |
| Traverse | O(n) |
| Search | O(n) |
| Insert at end | O(1) average |
| Insert at beginning | O(n) |
| Delete from beginning | O(n) |
Access and update are fast because an index gives the exact position. Inserting or deleting at the beginning is slower because the remaining elements must shift.
Real-life example
Imagine a classroom attendance register where each student has a roll number. The teacher can go directly to roll number 15, just as code accesses an array by index. Adding a new student at the beginning requires every existing position to move, just like an O(n) insertion at the start of an array.
Advantages of Arrays
- Easy to understand and use
- Fast access through indexes
- Efficient memory usage
- Ideal for ordered data
- Available in almost every programming language
Limitations of Arrays
- Insertion near the beginning or middle can be slow
- Deletion may require shifting elements
- Searching an unsorted array usually checks values one by one
- Frequent insertion and deletion may call for a linked list, stack, or queue instead
Where are Arrays used?
- Student records
- Online product lists
- Music playlists
- Image pixels
- Game scoreboards
- Calendar events
- Banking transactions
- Search results
Tips for beginners
- Remember that indexing usually begins at zero
- Practice accessing and updating values
- Use loops to traverse arrays
- Learn when insertion and deletion are fast or slow
- Solve small array problems regularly
Key takeaways
- An array stores multiple values in one variable
- Every element has an index
- Index access and updates are O(1)
- Traversal and unsorted search are O(n)
- Beginning insertions and deletions require shifting elements
- Arrays form the foundation of many advanced structures and algorithms