What is an Array?
- Array is a collection of elements, each identified by an index or key.
- Arrays have a fixed & dynamic size
- Homogeneous elements → all elements in an array are of the same data type.
- Heterogeneous elements → allowing different data types in the same array.
// Homogeneous
int[] intArray = new int[5]; // Array of integers String[]
stringArray = new String[5]; // Array of strings
// Heterogeneous
mixedArray = [1, "hello", 3.14, True] # Mixed data types in one list
Characteristics of Arrays
- Indexing: Zero-based indexing in most programming languages.
- Size: Fixed size (static), cannot be changed dynamically (except in languages with dynamic arrays).
- Memory Allocation: Contiguous memory allocation for array elements. meaning each element is directly next to the previous one in memory. This is possible because all elements are of the same size, allowing the system to calculate the memory address of any element using its index.

Array Operations
- Insertion: Typically involves shifting elements, O(n) time complexity.
- Deletion: Similar to insertion, elements may need to be shifted. Except last index
- Traversal: Iterating through all elements, O(n) time complexity.
- Access Time: O(1) time complexity for accessing an element using its index.
Types of Arrays
- One-dimensional Array: Simplest form, like a list.
- Multi-dimensional Array: Arrays of arrays (e.g., 2D array).
- Jagged Array: Arrays with different lengths of sub-arrays.
- Dynamic Arrays (e.g., ArrayList in Java): Arrays that can grow in size dynamically.
Advantages of Arrays
- Efficiency: O(1) access time for elements.
- Memory Utilization: Efficient use of memory due to contiguous storage.
- Ease of Use: Simplifies data management and operations like sorting and searching
Disadvantages of Arrays
- Fixed Size: Cannot change size once declared. except dynamic array
- Insertion/Deletion Cost: O(n) for inserting or deleting an element, especially in the middle.
- Memory Wastage: Unused elements still occupy space.
Real-world Applications of Arrays
- Storing Data: Common in programming for storing collections of elements.
- Sorting Algorithms: Many sorting algorithms are designed for arrays (e.g., QuickSort, MergeSort).
- Matrix Operations: 2D arrays are used for matrix operations in mathematics and graphics.
- Implementing Stacks and Queues: Basic data structures can be implemented using arrays.
Best Practices with Arrays
- Avoid Unnecessary Copies: Be mindful of operations that require copying elements.
- Use Dynamic Arrays When Needed: If size is uncertain, prefer dynamic arrays.
- Leverage Built-in Functions: Utilize language-specific functions for array operations.
- Boundary Checking: Always check for boundary conditions to avoid IndexOutOfBoundsException.
Static & Dynamic Array example in GO
package main
import (
"fmt"
"unsafe"
)
func main() {
// Static Array
var staticArr [5]int64
staticArr[0] = 1
staticArr[1] = 2
staticArr[2] = 3
staticArr[3] = 4
staticArr[4] = 5
elementSize := unsafe.Sizeof(staticArr[0])
totalSize := elementSize * uintptr(len(staticArr))
fmt.Printf("Memory used by static array: %d bytes\n", totalSize)
fmt.Println()
// Dynamic Array (Slice)
dynamicArr := make([]int32, 0, 5)
before := unsafe.Sizeof(dynamicArr[0])
beforeTotal := before * uintptr(len(dynamicArr))
fmt.Printf("Memory used by dynamic array (before): %d bytes\n", beforeTotal)
// Append elements to dynamic array
for i := 0; i