DEV Community

Jayaprasanna Roddam
Jayaprasanna Roddam

Posted on

Arrays

1. Introduction to Arrays

Definition:

An array is a collection of elements, all of the same type, stored in contiguous memory locations. It provides a way to store multiple items of the same data type in a single variable, allowing for efficient indexing and access.

Characteristics of Arrays:

  • Fixed Size: Once an array is created, its size cannot be changed (static arrays).
  • Homogeneous Elements: All elements in an array are of the same type.
  • Random Access: Elements can be accessed directly using their index.
  • Memory Layout: Arrays are stored in contiguous memory, allowing efficient access.

2. Accessing Array Elements

Syntax:

In most programming languages, array elements are accessed using an index that starts from 0. For example, in Go:

arr := []int{10, 20, 30, 40, 50}
fmt.Println(arr[0]) // Output: 10
Enter fullscreen mode Exit fullscreen mode

Example Problem:

Given an array of integers, return the first and last elements.

func firstAndLast(arr []int) (int, int) {
    return arr[0], arr[len(arr)-1]
}

// Example usage
arr := []int{1, 2, 3, 4, 5}
first, last := firstAndLast(arr)
// Output: first = 1, last = 5
Enter fullscreen mode Exit fullscreen mode

3. Insertion and Deletion in Arrays

Insertion:

  • At the End: Inserting an element at the end of the array is straightforward (if there's space).
  • At a Specific Index: Requires shifting elements to make space.

Deletion:

  • From the End: Simply remove the last element.
  • From a Specific Index: Requires shifting elements to fill the gap.

Example Problem:

Implement a function to insert an element at a specific index in an array.

func insertAtIndex(arr []int, index int, value int) []int {
    arr = append(arr[:index], append([]int{value}, arr[index:]...)...)
    return arr
}

// Example usage
arr := []int{1, 2, 3, 4, 5}
arr = insertAtIndex(arr, 2, 99)
// Output: arr = [1, 2, 99, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

4. Traversal of Arrays

Definition:

Traversal is the process of accessing each element of an array exactly once for processing. It is commonly done using loops.

Example Problem:

Write a function to print all elements of an array.

func printArray(arr []int) {
    for _, value := range arr {
        fmt.Println(value)
    }
}

// Example usage
arr := []int{1, 2, 3, 4, 5}
printArray(arr)
// Output: 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

5. Static vs. Dynamic Arrays

Static Arrays:

  • Size is fixed at compile time.
  • Memory is allocated on the stack.
  • Example: int arr[5]; in C/C++.

Dynamic Arrays:

  • Size can be determined at runtime.
  • Memory is allocated on the heap.
  • In languages like Go, dynamic arrays are implemented using slices.

Example Problem:

Compare the usage of static and dynamic arrays.

// Static array (size fixed at compile time)
var staticArr [5]int

// Dynamic array (size determined at runtime)
dynamicArr := make([]int, 0)
dynamicArr = append(dynamicArr, 1, 2, 3)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)