Sajan Chandran
Sajan Chandran

Reputation: 11487

Remove elements in slice

I am complete newbie in Golang, I am trying to remove elements in one slice based on the elements in another slice. e.g.

input slice : urlList := []string{"test", "abc", "def", "ghi"}

elements to remove slice : remove := []string{"abc", "test"}

expected output slice : urlList := []string{"def", "ghi"}

This is what I tried.

func main() {

    urlList := []string{"test", "abc", "def", "ghi"}
    remove := []string{"abc", "test"}
loop:
    for i, url := range urlList {
        for _, rem := range remove {
            if url == rem {
                urlList = append(urlList[:i], urlList[i+1:]...)
                continue loop
            }
        }
    }
    for _, v := range urlList {
        fmt.Println(v)
    }
}

But it's not working as I expected. I don't know what I am missing.

Upvotes: 4

Views: 22813

Answers (6)

Roman
Roman

Reputation: 11

Try it

https://go.dev/play/p/CKvGWl7vG4_V

elements := []string{"one", "two", "three", "four"}
needToRemove := []string{"one", "four"}

newSlice := remove(elements, needToRemove) // []string{"two", "three"}


func remove(slice, elements []string) []string {
    out := []string{}
    bucket := map[string]bool{}

    for _, element := range slice {
        if !inSlice(elements, element) && !bucket[element] {
            out = append(out, element)
            bucket[element] = true
        }
    }

    return out
}

func inSlice(slice []string, elem string) bool {
    for _, i := range slice {
        if i == elem {
            return true
        }
    }

    return false
}

Upvotes: 0

alessiosavi
alessiosavi

Reputation: 3037

In case of index:

//RemoveElements delete the element of the indexes contained in j of the data in input
func RemoveElements(data []string, j []int) []string {
    var newArray []string
    var toAdd bool = true
    var removed int = 0
    //sort.Ints(j)
    for i := 0; i < len(data); i++ {
        for _, k := range j {
            // if k < i || k > i {
            //  break
            // } else
            if i == k {
                toAdd = false
                break
            }
        }
        if toAdd {
            newArray = append(newArray, data[i])
            removed++
        }
        toAdd = true
    }
    return newArray
}

Comment can be removed for increase performance when the slice is not so big (sort time)

Upvotes: 0

LEMUEL  ADANE
LEMUEL ADANE

Reputation: 8818

You can use these functions:

func main() {
  array := []string{"A", "B", "C", "D", "E"}
  a = StringSliceDelete(a, 2) // delete "C"
  fmt.Println(a) // print: [A, B, D E]
}

//IntSliceDelete function
func IntSliceDelete(slice []int, index int) []int {
   copy(slice[index:], slice[index+1:])
   new := slice[:len(slice)-1]
   return new
}

//StringSliceDelete function
func StringSliceDelete(slice []string, index int) []string {
    copy(slice[index:], slice[index+1:])
    new := slice[:len(slice)-1]
    return new
}

// ObjectSliceDelete function
func ObjectSliceDelete(slice []interface{}, index int) []interface{} {
    copy(slice[index:], slice[index+1:])
    new := slice[:len(slice)-1]
    return new
}

Upvotes: 0

Paul Hankin
Paul Hankin

Reputation: 58211

You have to be careful when modifying a slice while iterating over it.

Here's a common way to remove elements from a slice by compacting the data at the same time as iterating over it.

It also uses a map rather than a slice for excluded elements, which gives efficiency when the number of excluded items is large.

Exclude updates xs in place, which is why a pointer argument is used. An alternative would be to update the backing array of xs, but to return the slice from the function in the same way that the built-in append works.

package main

import "fmt"

func Exclude(xs *[]string, excluded map[string]bool) {
    w := 0
    for _, x := range *xs {
        if !excluded[x] {
            (*xs)[w] = x
            w++
        }
    }
    *xs = (*xs)[:w]
}

func mapFromSlice(ex []string) map[string]bool {
    r := map[string]bool{}
    for _, e := range ex {
        r[e] = true
    }
    return r
}

func main() {
    urls := []string{"test", "abc", "def", "ghi"}
    remove := mapFromSlice([]string{"abc", "test"})
    Exclude(&urls, remove)
    fmt.Println(urls)
}

This code is O(N+M) in run-time where N is the length of urls and M is the length of remove.

Upvotes: 1

icza
icza

Reputation: 417472

The problem is that when you remove an element from the original list, all subsequent elements are shifted. But the range loop doesn't know that you changed the underlying slice and will increment the index as usual, even though in this case it shouldn't because then you skip an element.

And since the remove list contains 2 elements which are right next to each other in the original list, the second one ("abc" in this case) will not be checked and will not be removed.

One possible solution is not to use range in the outer loop, and when you remove an element, you manually decrease the index i-- because continuing with the next iteration it will get auto-incremented:

urlList := []string{"test", "abc", "def", "ghi"}
remove := []string{"abc", "test"}

loop:
for i := 0; i < len(urlList); i++ {
    url := urlList[i]
    for _, rem := range remove {
        if url == rem {
            urlList = append(urlList[:i], urlList[i+1:]...)
            i-- // Important: decrease index
            continue loop
        }
    }
}

fmt.Println(urlList)

Output:

[def ghi]

Note:

Since the outer loop contains nothing after the inner loop, you can replace the label+continue with a simple break:

urlList := []string{"test", "abc", "def", "ghi"}
remove := []string{"abc", "test"}

for i := 0; i < len(urlList); i++ {
    url := urlList[i]
    for _, rem := range remove {
        if url == rem {
            urlList = append(urlList[:i], urlList[i+1:]...)
            i-- // Important: decrease index
            break
        }
    }
}

fmt.Println(urlList)

Try it on Go Playground.

Alternative

An alternative to this would be for the outer loop to go downward, so no need to manually decrease (or increase) the index variable because the shifted elements are not affected (already processed due to the downward direction).

Upvotes: 9

Akavall
Akavall

Reputation: 86138

Maybe it is simpler to create a new slice, that only contains your desired elements, for example:

package main

import "fmt"

func main() {
    urlList := []string{"test", "abc", "def", "ghi"}
    remove := []string{"abc", "test"}

    new_list := make([]string, 0)

    my_map := make(map[string]bool, 0)
    for _, ele := range remove {
        my_map[ele] = true
    }

    for _, ele := range urlList {
        _, is_in_map := my_map[ele]
        if is_in_map {
            fmt.Printf("Have to ignore : %s\n", ele)
        } else {
            new_list = append(new_list, ele)    
        }
    }

    fmt.Println(new_list)

}

playground

Result:

Have to ignore : test
Have to ignore : abc
[def ghi]

Upvotes: 3

Related Questions