Home  >  Article  >  Backend Development  >  How to implement set in golang

How to implement set in golang

PHPz
PHPzOriginal
2023-04-14 09:16:371035browse

Golang is a statically typed, high-concurrency, object-oriented programming language, and it is also one of the languages ​​that has become increasingly popular in recent years. In Golang, although there is no Set type, we can easily implement the function of Set through basic data structures such as slice and map. This article will introduce how to implement Set with Golang.

Definition of Set

Set is an unordered and non-repeating data structure. It supports basic operations of the set, including adding elements, deleting elements, determining whether elements exist, etc. In a Set, there is no sequential relationship between elements, and each element is unique.

Slice implements Set

Slice in Golang can implement the function of Set, because the elements in Slice are ordered and each element can appear repeatedly. We can implement the deduplication operation of Slice through custom methods to achieve the purpose of Set.

type SetSlice []interface{}

func (s *SetSlice) Add(val interface{}) {
    for _, v := range *s {
        if val == v {
            return
        }
    }
    *s = append(*s, val)
}

func (s *SetSlice) Remove(val interface{}) {
    for i, v := range *s {
        if val == v {
            *s = append((*s)[:i], (*s)[i+1:]...)
            return
        }
    }
}

func (s *SetSlice) Contains(val interface{}) bool {
    for _, v := range *s {
        if val == v {
            return true
        }
    }
    return false
}

In the above code, we customized the SetSlice type and added Add, Remove, Contains and other methods. When calling the Add method, we first check whether the same element exists by traversing the Slice, and return directly if it exists; otherwise, add the element to the Slice; when deleting an element, we traverse the Slice to find the element to be deleted and perform the deletion operation; When determining whether an element exists, we also traverse the Slice to find the element.

Map implements Set

In addition to Slice, Map in Golang can also implement the function of Set, because each key in the Map must be unique. We can use the key of the Map as the value of the element, and set the value of the Map to any value. We don't care what the specific value is, we only need to determine whether the element appears in the Map.

type SetMap map[interface{}]struct{}

var exist = struct{}{}

func (s SetMap) Add(val interface{}) {
    s[val] = exist
}

func (s SetMap) Remove(val interface{}) {
    delete(s, val)
}

func (s SetMap) Contains(val interface{}) bool {
    _, c := s[val]
    return c
}

In the above code, we customized the SetMap type and added Add, Remove, Contains and other methods. When calling the Add method, we directly insert the element into the Map as the key of the Map, and the value is an empty struct{} type; when deleting the element, we directly delete the corresponding key in the Map through the delete function; when judging whether the element exists, We determine whether the element exists by accessing the corresponding key in the Map.

Summary

In Golang, although there is no Set type, we can realize the function of Set through basic data structures such as slice and map. Using slice to implement Set requires manual deduplication, which is more troublesome; using map to implement Set is simpler and more efficient. Of course, if we need to perform more complex operations on Set, such as intersection and union, it is recommended to use a third-party library, such as github.com/deckarep/golang-set, which can greatly improve efficiency.

The above is the detailed content of How to implement set in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn