Best way to remove selected elements from slice


Akhil K Nambiar

I have one slice A and another slice B. Slice A contains n elements, slice B is a subset of slice A, where each element is a pointer to slice A.

What is the cheapest way to remove all elements in A from B?

After some googling, the only way I can think of is to re-slice A for each element in B. Is this the only way, or is there an easier way?

peterSO :

I have one slice A and another slice B. Slice A contains n elements, slice B is a subset of slice A, where each element is a pointer to slice A.

What is the cheapest way to remove all elements in A from B?

A and B may have duplicates and may not be sorted.


For example, the growth rate O(n),

package main

import "fmt"

func remove(a []int, b []*int) []int {
    d := make(map[*int]bool, len(b))
    for _, e := range b {
        d[e] = true
    }
    var c []int
    if len(a) >= len(d) {
        c = make([]int, 0, len(a)-len(d))
    }
    for i := range a {
        if !d[&a[i]] {
            c = append(c, a[i])
        }
    }
    return c
}

func main() {
    a := []int{0, 1, 2, 3, 4, 5, 6, 7}
    fmt.Println(a)
    b := []*int{&a[1], &a[3], &a[3], &a[7], &a[4]}
    a = remove(a, b)
    fmt.Println(a)
}

Playground : https://play.golang.org/p/-RpkH51FSt2 _

output:

[0 1 2 3 4 5 6 7]
[0 2 5 6]

Related


Best way to remove selected elements from slice

Akhil K Nambiar I have one slice A and another slice B. Slice A contains n elements, slice B is a subset of slice A, where each element is a pointer to slice A. What is the cheapest way to remove all elements in A from B? After some googling, the only way I ca

Best way to remove selected elements from slice

Akhil K Nambiar I have one slice A and another slice B. Slice A contains n elements, slice B is a subset of slice A, where each element is a pointer to slice A. What is the cheapest way to remove all elements in A from B? After some googling, the only way I ca

Best way to remove elements from a list

username I would like to know what is the best/efficient way to remove elements from a list. There are some functions provided by Python: some_list.remove(value), but raises an error if the value is not found. some_list.pop(some_list[index]), deletes the item

remove elements from slice

Sax: Go doesn't provide any advanced functionality to remove elements from slices. I wrote a function that removes a given value from a slice in the way that is usually suggested here, but it produces very unexpected results. package main import "fmt" type A

remove elements from slice

Jorge Olivero: func main() { a := []string{"Hello1", "Hello2", "Hello3"} fmt.Println(a) // [Hello1 Hello2 Hello3] a = append(a[:0], a[1:]...) fmt.Println(a) // [Hello2 Hello3] } How does this delete trick with add function work? Seems

remove elements from slice

Sajan Chandran I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Exp

remove elements from slice

Sajan Chandran: I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Ex

remove elements from slice

Jorge Olivero: func main() { a := []string{"Hello1", "Hello2", "Hello3"} fmt.Println(a) // [Hello1 Hello2 Hello3] a = append(a[:0], a[1:]...) fmt.Println(a) // [Hello2 Hello3] } How does this delete trick with add function work? Seems

remove elements from slice

Sax: Go doesn't provide any advanced functionality to remove elements from slices. I wrote a function that removes a given value from a slice in the way that is usually suggested here, but it produces very unexpected results. package main import "fmt" type A

remove elements from slice

Jorge Olivero: func main() { a := []string{"Hello1", "Hello2", "Hello3"} fmt.Println(a) // [Hello1 Hello2 Hello3] a = append(a[:0], a[1:]...) fmt.Println(a) // [Hello2 Hello3] } How does this delete trick with add function work? Seems

remove elements from slice

Sajan Chandran I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Exp

remove elements from slice

Sax: Go doesn't provide any advanced functionality to remove elements from slices. I wrote a function that removes a given value from a slice in the way that is usually suggested here, but it produces very unexpected results. package main import "fmt" type A

remove elements from slice

Jorge Olivero: func main() { a := []string{"Hello1", "Hello2", "Hello3"} fmt.Println(a) // [Hello1 Hello2 Hello3] a = append(a[:0], a[1:]...) fmt.Println(a) // [Hello2 Hello3] } How does this delete trick with add function work? Seems

remove elements from slice

Sajan Chandran: I'm completely new to Golang and I'm trying to delete an element in one slice based on an element in another slice. E.g Input slice:urlList := []string{"test", "abc", "def", "ghi"} Remove elements of a slice:remove := []string{"abc", "test"} Ex

Best way to group every two elements in a slice?

User 9503053: Suppose you have a small string in it with letters like this: a b c d e f g h How would you go through the list and group every two elements together? I am trying to save the first element in a variable and the next element in another variable. S

Best way to group every two elements in a slice?

User 9503053: Suppose you have a small string in it with letters like this: a b c d e f g h How would you go through the list and group every two elements together? I am trying to save the first element in a variable and the next element in another variable. S

Best way to group every two elements in a slice?

User 9503053: Suppose you have a small string in it with letters like this: a b c d e f g h How would you go through the list and group every two elements together? I am trying to save the first element in a variable and the next element in another variable. S

Best way to remove elements from Ruby hash array in Yaml

Ben I've seen a lot online about removing keys from yaml , but not much about removing elements from hash arrays (maybe I'm searching for the wrong concept?). Sorry, I'm pretty new to Ruby or anything related to this. I am trying to remove a specific port from

Best way to remove elements from Ruby hash array in Yaml

Ben I've seen a lot online about removing keys from yaml , but not much about removing elements from hash arrays (maybe I'm searching for the wrong concept?). Sorry, I'm pretty new to Ruby or anything related to this. I am trying to remove a specific port from

Remove selected elements from an array

Rohit Darvi I have input feild which takes the input ( ) interestfrom the user and adds it to an array after the enter key is pressed . Then , as the user continues to add , the elements in that array are displayed on the screen through the component . The com