Remove elements from slice

suggest change

We use []int in our examples but the code works for slices of all types.

Remove a single element at a given index

	s := []int{10, 11, 12, 13}

	i := 2 // index of 12
	s = append(s[:i], s[i+1:]...)

	fmt.Printf("s: %#v\n", s)
s: []int{10, 11, 13}

Remove multiple elements

	s := []int{10, 11, 12, 13}

	i := 1 // index of 11
	n := 2 // remove 2 elements

	s = append(s[:i], s[i+n:]...)

	fmt.Printf("s: %#v\n", s)
s: []int{10, 13}

Efficiency of removal

Note on efficiency: Go compiler is smart enough to reuse the space in the original slice, so this method is quite efficient. It doesn't allocate new space and merely copies the elements within the slice. We can verify this:

	s := []int{10, 11, 12, 13}
	fmt.Printf("&s[0]: %p, cap(s): %d\n", &s[0], cap(s))
	fmt.Printf("s: %#v\n", s)

	i := 1 // index of 11
	n := 2 // remove 2 elements

	s = append(s[:i], s[i+n:]...)

	fmt.Print("\nAfter removal:\n")
	fmt.Printf("&s[0]: %p, cap(s): %d\n", &s[0], cap(s))
	fmt.Printf("s: %#v\n", s)

	s = append(s, 1, 2, 3, 4)
	fmt.Printf("\nAfter appending beyond remaining capacity:\n")
	fmt.Printf("&s[0]: %p, cap(s): %d\n", &s[0], cap(s))
	fmt.Printf("s: %#v\n", s)
&s[0]: 0xc0000140a0, cap(s): 4
s: []int{10, 11, 12, 13}

After removal:
&s[0]: 0xc0000140a0, cap(s): 4
s: []int{10, 13}

After appending beyond remaining capacity:
&s[0]: 0xc000074000, cap(s): 8
s: []int{10, 13, 1, 2, 3, 4}

The %p formatting directive prints the address of the first element. The unchanged address and capacity show that removal reused the backing array.

Appending four elements exceeds the remaining capacity of two, so append allocates a new backing array in this example.

Optimized, in-place removal

If we don't care to preserve the order of elements, we can optimize removal even further:

	s := []int{10, 11, 12, 13}

	i := 1 // index of 11
	lastIdx := len(s) - 1

	s[i] = s[lastIdx]
	s = s[:lastIdx]

	fmt.Printf("s: %#v\n", s)
s: []int{10, 13, 12}

We overwrite the element we want to remove with the last element in the slice and shrink the slice by 1.

This copies one element instead of shifting every element after i. The difference matters most for large slices. Both approaches require i to be a valid index; otherwise the program panics.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents