How to delete an element from a Slice in Golang

前端 未结 14 1449
时光取名叫无心
时光取名叫无心 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:20

    The best way to do it is to use the append function:

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        x := []int{4, 5, 6, 7, 88}
        fmt.Println(x)
        x = append(x[:2], x[4:]...)//deletes 6 and 7
        fmt.Println(x)
    }
    

    https://play.golang.org/p/-EEFCsqse4u

    0 讨论(0)
  • 2021-01-30 03:21

    Minor point (code golf), but in the case where order does not matter you don't need to swap the values. Just overwrite the array position being removed with a duplicate of the last position and then return a truncated array.

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

    Same result.

    0 讨论(0)
  • 2021-01-30 03:21

    Maybe this code will help.

    It deletes item with a given index.

    Takes the array, and the index to delete and returns a new array pretty much like append function.

    func deleteItem(arr []int, index int) []int{
      if index < 0 || index >= len(arr){
        return []int{-1}
      }
    
        for i := index; i < len(arr) -1; i++{
          arr[i] = arr[i + 1]
    
        }
    
        return arr[:len(arr)-1]
    }
    

    Here you can play with the code : https://play.golang.org/p/aX1Qj40uTVs

    0 讨论(0)
  • 2021-01-30 03:27

    To remove an element from the middle of a slice, preserving the order of the remaining elements, use copy to slide the higher-numbered elements down by one to fill the gap:

    func remove(slice []int, i int) []int {
      copy(slice[i:], slice[i+1:])
      return slice[:len(slice)-1]
    }
    

    If it is not necessary to preserve the order, we can simply move the last element to the gap.

    func remove(slice []int, i int) []int {
      slice[i] = slice[len(slice)-1]
      return slice[:len(slice)-1]
    }
    
    0 讨论(0)
  • 2021-01-30 03:30

    Remove one element from the Slice (this is called 're-slicing'):

    package main
    
    import (
        "fmt"
    )
    
    func RemoveIndex(s []int, index int) []int {
        return append(s[:index], s[index+1:]...)
    }
    
    func main() {
        all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
        fmt.Println(all) //[0 1 2 3 4 5 6 7 8 9]
        all = RemoveIndex(all, 5)
        fmt.Println(all) //[0 1 2 3 4 6 7 8 9]
    }
    
    0 讨论(0)
  • 2021-01-30 03:33

    This is how you Delete From a slice the idiomatic way. You don't need to build a function it is built into the append. Try it here https://play.golang.org/p/QMXn9-6gU5P

    z := []int{9, 8, 7, 6, 5, 3, 2, 1, 0}
    fmt.Println(z)  //will print Answer [9 8 7 6 5 3 2 1 0]
    
    z = append(z[:2], z[4:]...)
    fmt.Println(z)   //will print Answer [9 8 5 3 2 1 0]
    
    0 讨论(0)
提交回复
热议问题