3.6 KiB
01: Array Basics
Overview
Array basics are the foundation of algorithmic thinking. Understanding how to traverse, manipulate, and analyze arrays is crucial for solving more complex problems.
Key Concepts
1. Array Traversal
What it is: Visiting each element in an array in sequence
Example:
// Linear traversal
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1; // Not found
}
Important considerations:
- Always check if array is empty first
- Use
forloops for precise index control - Use
forEachormapwhen you need the value but not the index
2. Basic Array Operations
Finding Minimum and Maximum
function findMinMax(arr: number[]): { min: number, max: number } {
if (arr.length === 0) {
throw new Error("Array is empty");
}
let min = arr[0];
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
return { min, max };
}
Time Complexity: O(n) - We visit each element once Space Complexity: O(1) - We only store a few variables
Frequency Counting
function frequencyCount(arr: number[]): Map<number, number> {
const frequency = new Map<number, number>();
for (const num of arr) {
frequency.set(num, (frequency.get(num) || 0) + 1);
}
return frequency;
}
Time Complexity: O(n) - One pass through the array Space Complexity: O(n) - Store frequency of each unique element
3. Edge Cases to Consider
- Empty array: What happens when
arr.length === 0? - Single element: Arrays with only one element
- All same elements: Arrays where all values are identical
- Negative numbers: How algorithms handle negative values
- Large arrays: Performance considerations
Practice Patterns
Pattern 1: Linear Search Variations
Problem: Find all occurrences of a target
function findAllOccurrences(arr: number[], target: number): number[] {
const indices: number[] = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
indices.push(i);
}
}
return indices;
}
Time Complexity: O(n) Space Complexity: O(k) where k is number of occurrences
Pattern 2: Array Validation
Problem: Check if array meets certain criteria
function isNonDecreasing(arr: number[]): boolean {
for (let i = 1; i < arr.length; i++) {
if (arr[i] < arr[i-1]) {
return false;
}
}
return true;
}
Time Complexity: O(n) Space Complexity: O(1)
Real-world Applications
- Search functionality: Finding items in a list
- Data analysis: Counting frequencies, finding trends
- Game development: Player positions, inventory management
- Web development: Form validation, data filtering
Common Mistakes
- Off-by-one errors: Using
<vs<=in loops - Undefined access: Not checking array bounds
- Memory leaks: Not clearing temporary arrays
- Inefficient algorithms: Using nested loops when unnecessary
Next Steps
After mastering array basics, you should be comfortable with:
- Traversing arrays in different ways
- Performing basic operations (search, count, min/max)
- Handling edge cases properly
- Understanding time complexity of basic operations
Ready for: Two pointers technique (next topic)