Skip to content

Instantly share code, notes, and snippets.

@iwinux
Forked from JeffreyZhao/List.cs
Created November 2, 2012 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwinux/4001512 to your computer and use it in GitHub Desktop.
Save iwinux/4001512 to your computer and use it in GitHub Desktop.
List.removeMultiple implemented in Golang
package main
import ("fmt")
type (
T interface{}
MyList []T
)
func (listp *MyList) removeMultiple(toRemove MyList) {
list := *listp
toRemoveMap := make(map[T]bool)
for _, item := range toRemove { toRemoveMap[item] = true }
i := 0
upper := len(list) - 1
for i <= upper {
if _, ok := toRemoveMap[list[i]]; ok {
list[i], list[upper] = list[upper], list[i]
upper--
} else {
i++
}
}
*listp = list[0:(upper + 1)]
}
func main() {
list := MyList{ 1, 0, 2, 4, "wtf", 1024, "test", 250 }
toRemove := MyList{ "wtf", 1, 4, 250 }
fmt.Println("Original:", list)
fmt.Println("Removing:", toRemove)
list.removeMultiple(toRemove)
fmt.Println("After:", list)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment