Skip to content

Array Copying

Copy array elements into a new storage block using shallow or deep copy semantics.

Array copying creates another array with the same elements. The copy may duplicate only the element references, or it may recursively duplicate the objects stored inside the array.

You use it when a later mutation should not change the original array storage.

Problem

Given an array AA of length nn, create a new array BB such that:

B[i]=A[i] B[i] = A[i]

for every valid index ii.

Algorithm

Copy each element into a new array.

copy_array(A):
    n = length(A)
    B = allocate(n)

    for i from 0 to n - 1:
        B[i] = A[i]

    return B

Example

Let

A=[8,3,5,1,9] A = [8, 3, 5, 1, 9]

Copying produces:

B=[8,3,5,1,9] B = [8, 3, 5, 1, 9]
indexAB
088
133
255
311
499

Changing B[2]B[2] afterward does not change A[2]A[2] for value types.

Correctness

The algorithm allocates a new array of the same length as the input. It then visits every valid index and writes A[i]A[i] into B[i]B[i]. Therefore, every position in the output has the same value as the corresponding position in the input.

Since the backing storage is different, later changes to the array slots of BB do not overwrite the slots of AA.

Complexity

operationtime
copyO(n)O(n)

Space usage:

O(n) O(n)

When to Use

Array copying is appropriate when:

  • the original array must be preserved
  • a function needs independent storage
  • resizing requires moving elements
  • snapshot-style behavior is needed

Be careful with shallow copies when elements are references. The array storage is copied, but the referenced objects may still be shared.

Implementation

def copy_array(a):
    b = [None] * len(a)

    for i in range(len(a)):
        b[i] = a[i]

    return b
func CopyArray[T any](a []T) []T {
    b := make([]T, len(a))
    copy(b, a)
    return b
}