Back to Catalog
Data Structures
Random Access
Arrays
Contiguous memory, O(1) index access. The fastest structure when you know where to look.
Intent & Description
Real-world Use Case
Storing a leaderboard of top 100 scores, holding pixel data for image processing, buffering bytes from a network stream, implementing a lookup table. Anywhere you iterate or index ā arrays win.
Source
š TL;DR
Arrays = contiguous memory, O(1) index access, best cache performance. Default choice for any ordered collection. Only reach for something else when middle insertions or dynamic growth are on the hot path.
Advantages
- O(1) random access by index ā unbeatable for positional reads
- Best cache performance of any data structure ā sequential memory = CPU prefetch heaven
- Minimal memory overhead ā just the elements, no per-node pointers
- Foundation of all other structures (stacks, queues, heaps, hash tables all use arrays internally)
Disadvantages
- Insert/delete at arbitrary position is O(n) ā all elements after the target must shift
- Static arrays are fixed-size ā overflow means manual reallocation
- Dynamic arrays (ArrayList, JS Array) double capacity on resize ā O(n) occasional spikes
- Wasted capacity after deletion unless you compact ā memory fragmentation over time
Implementation Example
// āāā Array fundamentals āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
const arr = [10, 20, 30, 40, 50];
// O(1) ā Direct index access (base_addr + i * size)
console.log(arr[2]); // 30 ā always instant
console.log(arr.at(-1)); // 50 ā negative index (ES2022)
// O(n) ā Linear search (unsorted array)
const idx = arr.indexOf(30); // 2 ā scans left-to-right
const has = arr.includes(30); // true
// O(1) amortized ā Push to end (dynamic resize doubles capacity)
arr.push(60); // [10, 20, 30, 40, 50, 60]
// O(n) ā Insert at beginning (shifts all elements right)
arr.unshift(5); // [5, 10, 20, 30, 40, 50, 60]
// O(1) ā Remove from end
arr.pop(); // [5, 10, 20, 30, 40, 50]
// O(n) ā Remove from beginning (shifts all elements left)
arr.shift(); // [10, 20, 30, 40, 50]
// O(n) ā Insert/delete in middle via splice
arr.splice(2, 0, 99); // [10, 20, 99, 30, 40, 50] ā insert at index 2
arr.splice(2, 1); // [10, 20, 30, 40, 50] ā delete at index 2
// āāā Two-pointer technique (avoid O(n²) with O(n) + array) āāā
// O(n) ā Reverse in-place using two pointers
function reverseInPlace(arr) {
let lo = 0, hi = arr.length - 1;
while (lo < hi) {
[arr[lo], arr[hi]] = [arr[hi], arr[lo]]; // Swap
lo++; hi--;
}
return arr;
}
// O(n) ā Remove duplicates from sorted array in-place (O(1) extra space)
function deduplicateSorted(arr) {
let write = 1;
for (let read = 1; read < arr.length; read++) {
if (arr[read] !== arr[read - 1]) arr[write++] = arr[read];
}
return arr.slice(0, write);
}
// āāā Sliding window (O(n) over O(n²) nested loops) āāāāāāāāāāāāā
// O(n) ā Max sum subarray of length k
function maxSumSubarray(arr, k) {
let windowSum = arr.slice(0, k).reduce((a, b) => a + b, 0);
let maxSum = windowSum;
for (let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k]; // Slide: add new, remove old ā O(1) per step
maxSum = Math.max(maxSum, windowSum);
}
return maxSum;
}