I have a slice A and another slice B. Slice A contains n elements and slice B is a subset of slice A where each element is a pointer to Slice A.
What would be the cheapest method to remove all elements from A which is referred in B.
A and B may have duplicates and may not be sorted.
For example, 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]