How to delete an element from a Slice in Golang

前端 未结 14 1681
时光取名叫无心
时光取名叫无心 2021-01-30 03:03
fmt.Println(\"Enter position to delete::\")
fmt.Scanln(&pos)

new_arr := make([]int, (len(arr) - 1))
k := 0
for i := 0; i < (len(arr) - 1); {
    if i != pos {
           


        
14条回答
  •  抹茶落季
    2021-01-30 03:37

    Order matters

    If you want to keep your array ordered, you have to shift all of the elements at the right of the deleting index by one to the left. Hopefully, this can be done easily in Golang:

    func remove(slice []int, s int) []int {
        return append(slice[:s], slice[s+1:]...)
    }
    

    However, this is inefficient because you may end up with moving all of the elements, which is costy.

    Order is not important

    If you do not care about ordering, you have the much faster possibility to swap the element to delete with the one at the end of the slice and then return the n-1 first elements:

    func remove(s []int, i int) []int {
        s[len(s)-1], s[i] = s[i], s[len(s)-1]
        return s[:len(s)-1]
    }
    

    With the reslicing method, emptying an array of 1 000 000 elements take 224s, with this one it takes only 0.06ns. I suspect that internally, go only changes the length of the slice, without modifying it.

    Edit 1

    Quick notes based on the comments below (thanks to them !).

    As the purpose is to delete an element, when the order does not matter a single swap is needed, the second will be wasted :

    func remove(s []int, i int) []int {
        s[i] = s[len(s)-1]
        // We do not need to put s[i] at the end, as it will be discarded anyway
        return s[:len(s)-1]
    }
    

    Also, this answer does not perform bounds-checking. It expects a valid index as input. This means that negative values or indices that are greater or equal to len(s) will cause Go to panic. Slices and arrays being 0-indexed, removing the n-th element of an array implies to provide input n-1. To remove the first element, call remove(s, 0), to remove the second, call remove(s, 1), and so on and so forth.

提交回复
热议问题