Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contains method for a slice

Tags:

slice

go

Is there anything similar to a slice.contains(object) method in Go without having to do a search through each element in a slice?

like image 340
vosmith Avatar asked May 07 '12 16:05

vosmith


People also ask

How do you add an element to a slice?

To add an element to a slice , you can use Golang's built-in append method. append adds elements from the end of the slice. The first parameter to the append method is a slice of type T . Any additional parameters are taken as the values to add to the given slice .

How do you check if an array contains a value in Golang?

example.go package main import "fmt" func main() { arr := [5]int{10, 20, 30, 40, 50} var element int = 40 var result bool = false for _, x := range arr { if x == element { result = true break } } if result { fmt. Print("Element is present in the array.") } else { fmt. Print("Element is not present in the array.") } }


12 Answers

Mostafa has already pointed out that such a method is trivial to write, and mkb gave you a hint to use the binary search from the sort package. But if you are going to do a lot of such contains checks, you might also consider using a map instead.

It's trivial to check if a specific map key exists by using the value, ok := yourmap[key] idiom. Since you aren't interested in the value, you might also create a map[string]struct{} for example. Using an empty struct{} here has the advantage that it doesn't require any additional space and Go's internal map type is optimized for that kind of values. Therefore, map[string] struct{} is a popular choice for sets in the Go world.

like image 63
tux21b Avatar answered Oct 06 '22 04:10

tux21b


No, such method does not exist, but is trivial to write:

func contains(s []int, e int) bool {
    for _, a := range s {
        if a == e {
            return true
        }
    }
    return false
}

You can use a map if that lookup is an important part of your code, but maps have cost too.

like image 24
Mostafa Avatar answered Oct 06 '22 03:10

Mostafa


Starting with Go 1.18, you can use the slices package – specifically the generic Contains function: https://pkg.go.dev/golang.org/x/exp/slices#Contains.

go get golang.org/x/exp/slices
import  "golang.org/x/exp/slices"
things := []string{"foo", "bar", "baz"}
slices.Contains(things, "foo") // true

Note that since this is outside the stdlib as an experimental package, it is not bound to the Go 1 Compatibility Promise™ and may change before being formally added to the stdlib.

like image 32
Adolfo Avatar answered Oct 06 '22 03:10

Adolfo


The sort package provides the building blocks if your slice is sorted or you are willing to sort it.

input := []string{"bird", "apple", "ocean", "fork", "anchor"}
sort.Strings(input)

fmt.Println(contains(input, "apple")) // true
fmt.Println(contains(input, "grow"))  // false

...

func contains(s []string, searchterm string) bool {
    i := sort.SearchStrings(s, searchterm)
    return i < len(s) && s[i] == searchterm
}

SearchString promises to return the index to insert x if x is not present (it could be len(a)), so a check of that reveals whether the string is contained the sorted slice.

like image 29
Henrik Aasted Sørensen Avatar answered Oct 06 '22 02:10

Henrik Aasted Sørensen


With Go 1.18+ we could use generics.

func Contains[T comparable](s []T, e T) bool {
    for _, v := range s {
        if v == e {
            return true
        }
    }
    return false
}
like image 20
Amar Avatar answered Oct 06 '22 02:10

Amar


Instead of using a slice, map may be a better solution.

simple example:

package main

import "fmt"


func contains(slice []string, item string) bool {
    set := make(map[string]struct{}, len(slice))
    for _, s := range slice {
        set[s] = struct{}{}
    }

    _, ok := set[item] 
    return ok
}

func main() {

    s := []string{"a", "b"}
    s1 := "a"
    fmt.Println(contains(s, s1))

}

http://play.golang.org/p/CEG6cu4JTf

like image 38
holys Avatar answered Oct 06 '22 03:10

holys


If the slice is sorted, there is a binary search implemented in the sort package.

like image 29
Matt K Avatar answered Oct 06 '22 04:10

Matt K


func Contain(target interface{}, list interface{}) (bool, int) {
    if reflect.TypeOf(list).Kind() == reflect.Slice || reflect.TypeOf(list).Kind() == reflect.Array {
        listvalue := reflect.ValueOf(list)
        for i := 0; i < listvalue.Len(); i++ {
            if target == listvalue.Index(i).Interface() {
                return true, i
            }
        }
    }
    if reflect.TypeOf(target).Kind() == reflect.String && reflect.TypeOf(list).Kind() == reflect.String {
        return strings.Contains(list.(string), target.(string)), strings.Index(list.(string), target.(string))
    }
    return false, -1
}
like image 21
Jim Hsiang Avatar answered Oct 06 '22 04:10

Jim Hsiang


You can use the reflect package to iterate over an interface whose concrete type is a slice:

func HasElem(s interface{}, elem interface{}) bool {
    arrV := reflect.ValueOf(s)

    if arrV.Kind() == reflect.Slice {
        for i := 0; i < arrV.Len(); i++ {

            // XXX - panics if slice element points to an unexported struct field
            // see https://golang.org/pkg/reflect/#Value.Interface
            if arrV.Index(i).Interface() == elem {
                return true
            }
        }
    }

    return false
}

https://play.golang.org/p/jL5UD7yCNq

like image 35
Ethan Kennedy Avatar answered Oct 06 '22 02:10

Ethan Kennedy


If it is not feasable to use a map for finding items based on a key, you can consider the goderive tool. Goderive generates a type specific implementation of a contains method, making your code both readable and efficient.

Example;

type Foo struct {
    Field1 string
    Field2 int
} 

func Test(m Foo) bool {
     var allItems []Foo
     return deriveContainsFoo(allItems, m)
}

To generate the deriveContainsFoo method:

  • Install goderive with go get -u github.com/awalterschulze/goderive
  • Run goderive ./... in your workspace folder

This method will be generated for deriveContains:

func deriveContainsFoo(list []Foo, item Foo) bool {
    for _, v := range list {
        if v == item {
            return true
        }
    }
    return false
}

Goderive has support for quite some other useful helper methods to apply a functional programming style in go.

like image 27
Alexander van Trijffel Avatar answered Oct 06 '22 04:10

Alexander van Trijffel


Not sure generics are needed here. You just need a contract for your desired behavior. Doing the following is no more than what you would have to do in other languages if you wanted your own objects to behave themselves in collections, by overriding Equals() and GetHashCode() for instance.

type Identifiable interface{
    GetIdentity() string
}

func IsIdentical(this Identifiable, that Identifiable) bool{
    return (&this == &that) || (this.GetIdentity() == that.GetIdentity())
}

func contains(s []Identifiable, e Identifiable) bool {
    for _, a := range s {
        if IsIdentical(a,e) {
            return true
        }
    }
    return false
}
like image 22
JonPen Avatar answered Oct 06 '22 03:10

JonPen


I think map[x]bool is more useful than map[x]struct{}.

Indexing the map for an item that isn't present will return false. so instead of _, ok := m[X], you can just say m[X].

This makes it easy to nest inclusion tests in expressions.

like image 34
Bill Burdick Avatar answered Oct 06 '22 02:10

Bill Burdick