Array
Arrays are the most fundamental data structure, providing contiguous memory storage for elements of the same type. This page covers common array patterns and problems.Overview
Arrays provide O(1) random access but O(n) insertion and deletion for arbitrary positions. TypeScript’s built-in array type is used with various algorithms and patterns.Common Patterns
Two Pointer Technique
Used for merging sorted arrays, finding pairs, or partitioning.In-Place Operations
Modifying arrays without extra space for O(1) space complexity.Matrix Operations
Working with 2D arrays for grid-based problems.Problems & Solutions
Problem 1: Rotate Matrix 90 Degrees
Rotate an n×n matrix 90 degrees clockwise.Problem 2: Next Greater Element
Find the next greater element for each element in an array.Problem 3: Merge Sorted Arrays
Merge two sorted arrays in-place.Problem 4: Array Difference
Find elements that are in one array but not the other (symmetric difference).Problem 5: Array Pair Sum
Maximize the sum of minimum values in pairs.Complexity Reference
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Access | O(1) | - |
| Search | O(n) | - |
| Insert (end) | O(1) | - |
| Insert (arbitrary) | O(n) | - |
| Delete (end) | O(1) | - |
| Delete (arbitrary) | O(n) | - |
| Sort | O(n log n) | O(1) - O(n) |
Key Takeaways
Use arrays when:
- You need constant-time random access
- The size is known or doesn’t change frequently
- Memory should be contiguous
Additional Resources
Source Code
View array problem implementations
Test Cases
Explore test coverage