Glossary

Array

An array is an ordered collection of elements accessed by a numeric index, stored in contiguous memory locations. It's the most fundamental data structure in programming.

Explanation

An array stores elements sequentially in memory, which means element[0] lives right next to element[1] in RAM. This contiguous layout is what makes array access so fast: given the starting address and the size of each element, the computer can calculate any element's address in O(1) time with simple arithmetic. This is called random access. Most languages offer zero-indexed arrays (the first element is at index 0), while a few use one-based indexing. In JavaScript, arrays are dynamic — they resize automatically and can hold mixed types. In languages like C or Java, arrays have a fixed size set at creation and are homogeneous (all elements share the same type). Dynamic arrays (like JavaScript arrays or Python lists) internally use a fixed-size buffer that doubles when capacity is exceeded, giving amortized O(1) appends. Arrays excel at: reading or writing an element by index (O(1)), iterating all elements in order (O(n)), and when memory layout matters for cache performance. They're poor at: inserting or deleting in the middle (O(n) because you have to shift elements), and searching for a value without an index (O(n) linear scan, unless sorted and using binary search). When AI generates code that puts things in arrays, it's making a bet that index-based access is what you'll use. If you find yourself frequently searching for items by value, checking membership, or inserting in the middle, a different data structure (set, map, linked list) may be more appropriate.

Code Example

javascript
// JavaScript array operations with time complexity

const nums = [10, 20, 30, 40, 50];

// O(1) — index access
console.log(nums[2]); // 30

// O(1) amortized — append to end
nums.push(60);

// O(n) — insert at beginning (shifts all elements)
nums.unshift(0);

// O(n) — find by value (linear scan)
const idx = nums.indexOf(30); // 3

// O(n log n) — sort in place
nums.sort((a, b) => a - b);

// O(n) — create new array (map doesn't mutate)
const doubled = nums.map(x => x * 2);

Why It Matters for Engineers

Understanding arrays means understanding memory. When you know an array is backed by contiguous memory, you understand why iterating an array is cache-friendly (each step is likely already loaded into the CPU cache), while jumping around a linked list is not. This knowledge lets you write code that's fast in practice, not just theoretically correct. Vibe-coded solutions often default to arrays everywhere. Knowing their O(n) insertion cost stops you from, say, using an array to maintain a sorted list of 100,000 items where you're inserting frequently — a case where a different structure or algorithm would be orders of magnitude faster.

Learn This In Practice

Go deeper with the full module on Beyond Vibe Code.

Data Structures Fundamentals → →