Skip to content
On this page

# Set ​

Set is a data container, like list, but elements of set is not duplicate.

## Source ​

- https://github.com/duke-git/lancet/blob/main/datastructure/set/set.go

## Usage ​

go

    import (
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    import (
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

## Index ​

- NewSet
- NewSetFromSlice
- Values
- Add
- AddIfNotExist
- AddIfNotExistBy
- Delete
- Contain
- ContainAll
- Clone
- Size
- Equal
- Iterate
- EachWithBreak
- IsEmpty
- Union
- Intersection
- SymmetricDifference
- Minus

## Documentation ​

### NewSet ​

Create a set instance

Signature:

go

    type Set[T comparable] map[T]bool
    func NewSet[T comparable](items ...T) Set[T]

    type Set[T comparable] map[T]bool
    func NewSet[T comparable](items ...T) Set[T]

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

### NewSetFromSlice ​

Create a set from slice

Signature:

go

    func NewSetFromSlice[T comparable](items []T) Set[T]

    func NewSetFromSlice[T comparable](items []T) Set[T]

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSetFromSlice([]int{1, 2, 2, 3})
        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSetFromSlice([]int{1, 2, 2, 3})
        fmt.Println(st.Values()) //1,2,3
    }

### Values ​

Return slice of all set data

Signature:

go

    func (s Set[T]) Values() []T

    func (s Set[T]) Values() []T

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

### Add ​

Add items to set

Signature:

go

    func (s Set[T]) Add(items ...T)

    func (s Set[T]) Add(items ...T)

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Values()) //1,2,3
    }

### AddIfNotExist ​

AddIfNotExist checks if item exists in the set, it adds the item to set and returns true if it does not exist in the set, or else it does nothing and returns false.

Signature:

go

    func (s Set[T]) AddIfNotExist(item T) bool

    func (s Set[T]) AddIfNotExist(item T) bool

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        r1 := st.AddIfNotExist(1)
        r2 := st.AddIfNotExist(4)

        fmt.Println(r1) // false
        fmt.Println(r2) // true
        fmt.Println(st.Values()) // 1,2,3,4
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        r1 := st.AddIfNotExist(1)
        r2 := st.AddIfNotExist(4)

        fmt.Println(r1) // false
        fmt.Println(r2) // true
        fmt.Println(st.Values()) // 1,2,3,4
    }

### AddIfNotExistBy ​

AddIfNotExistBy checks if item exists in the set and pass the `checker` function it adds the item to set and returns true if it does not exists in the set and function `checker` returns true, or else it does nothing and returns false.

Signature:

go

    func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool

    func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2)

        ok := st.AddIfNotExistBy(3, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(ok) // true

        notOk := st.AddIfNotExistBy(4, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(notOk) // false

        fmt.Println(st.Values()) // 1, 2, 3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2)

        ok := st.AddIfNotExistBy(3, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(ok) // true

        notOk := st.AddIfNotExistBy(4, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(notOk) // false

        fmt.Println(st.Values()) // 1, 2, 3
    }

### Delete ​

Delete item in set

Signature:

go

    func (s Set[T]) Delete(items ...T)

    func (s Set[T]) Delete(items ...T)

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        set.Delete(3)
        fmt.Println(st.Values()) //1,2
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        set.Delete(3)
        fmt.Println(st.Values()) //1,2
    }

### Contain ​

Check if item is in set or not

Signature:

go

    func (s Set[T]) Contain(item T) bool

    func (s Set[T]) Contain(item T) bool

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Contain(1)) //true
        fmt.Println(st.Contain(4)) //false
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Contain(1)) //true
        fmt.Println(st.Contain(4)) //false
    }

### ContainAll ​

Checks if set contains another set

Signature:

go

    func (s Set[T]) ContainAll(other Set[T]) bool

    func (s Set[T]) ContainAll(other Set[T]) bool

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.ContainAll(set2)) //true
        fmt.Println(set1.ContainAll(set3)) //false
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.ContainAll(set2)) //true
        fmt.Println(set1.ContainAll(set3)) //false
    }

### Size ​

Get the number of elements in set

Signature:

go

    func (s Set[T]) Size() int

    func (s Set[T]) Size() int

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)

        fmt.Println(set1.Size()) //3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)

        fmt.Println(set1.Size()) //3
    }

### Clone ​

Make a copy of set

Signature:

go

    func (s Set[T]) Clone() Set[T]

    func (s Set[T]) Clone() Set[T]

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set1.Clone()

        fmt.Println(set1.Size() == set2.Size()) //true
        fmt.Println(set1.ContainAll(set2)) //true
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set1.Clone()

        fmt.Println(set1.Size() == set2.Size()) //true
        fmt.Println(set1.ContainAll(set2)) //true
    }

### Equal ​

Check if two sets has same elements or not

Signature:

go

    func (s Set[T]) Equal(other Set[T]) bool

    func (s Set[T]) Equal(other Set[T]) bool

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2, 3)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.Equal(set2)) //true
        fmt.Println(set1.Equal(set3)) //false
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2, 3)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.Equal(set2)) //true
        fmt.Println(set1.Equal(set3)) //false
    }

### Iterate ​

Call function by every element of set

Signature:

go

    func (s Set[T]) Iterate(fn func(item T))

    func (s Set[T]) Iterate(fn func(item T))

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        arr := []int{}
        set.Iterate(func(item int) {
            arr = append(arr, item)
        })

        fmt.Println(arr) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        arr := []int{}
        set.Iterate(func(item int) {
            arr = append(arr, item)
        })

        fmt.Println(arr) //1,2,3
    }

### EachWithBreak ​

Iterates over elements of a set and invokes function for each element, when iteratee return false, will break the for each loop.

Signature:

go

    func (s Set[T]) EachWithBreak(iteratee func(item T) bool)

    func (s Set[T]) EachWithBreak(iteratee func(item T) bool)

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet(1, 2, 3, 4, 5)

        var sum int

        s.EachWithBreak(func(n int) bool {
            if n > 3 {
                return false
            }
            sum += n
            return true
        })

        fmt.Println(sum) //6
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet(1, 2, 3, 4, 5)

        var sum int

        s.EachWithBreak(func(n int) bool {
            if n > 3 {
                return false
            }
            sum += n
            return true
        })

        fmt.Println(sum) //6
    }

### IsEmpty ​

Check if the set is empty or not

Signature:

go

    func (s Set[T]) IsEmpty() bool

    func (s Set[T]) IsEmpty() bool

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet()

        fmt.Println(set1.IsEmpty()) //false
        fmt.Println(set2.IsEmpty()) //true
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet()

        fmt.Println(set1.IsEmpty()) //false
        fmt.Println(set2.IsEmpty()) //true
    }

### Union ​

Create a new set contain all element of set s and other

Signature:

go

    func (s Set[T]) Union(other Set[T]) Set[T]

    func (s Set[T]) Union(other Set[T]) Set[T]

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Union(set2)

        fmt.Println(set3.Values()) //1,2,3,4,5
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Union(set2)

        fmt.Println(set3.Values()) //1,2,3,4,5
    }

### Intersection ​

Create a new set whose element both be contained in set s and other

Signature:

go

    func (s Set[T]) Intersection(other Set[T]) Set[T]

    func (s Set[T]) Intersection(other Set[T]) Set[T]

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Intersection(set2)

        fmt.Println(set3.Values()) //2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Intersection(set2)

        fmt.Println(set3.Values()) //2,3
    }

### SymmetricDifference ​

Create a new set whose element is in set1 or set2, but not in both set1 and set2

Signature:

go

    func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]

    func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.SymmetricDifference(set2)

        fmt.Println(set3.Values()) //1,4,5
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.SymmetricDifference(set2)

        fmt.Println(set3.Values()) //1,4,5
    }

### Minus ​

Create an set of whose element in origin set but not in compared set

Signature:

go

    func (s Set[T]) Minus(comparedSet Set[T]) Set[T]

    func (s Set[T]) Minus(comparedSet Set[T]) Set[T]

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set.NewSet(2, 3)

        res1 := set1.Minus(set2)
        fmt.Println(res1.Values()) //1

        res2 := set2.Minus(set3)
        fmt.Println(res2.Values()) //4,5
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set.NewSet(2, 3)

        res1 := set1.Minus(set2)
        fmt.Println(res1.Values()) //1

        res2 := set2.Minus(set3)
        fmt.Println(res2.Values()) //4,5
    }

### Pop ​

Delete the top element of set then return it, if set is empty, return nil-value of T and false.

Signature:

go

    func (s Set[T]) Pop() (v T, ok bool)

    func (s Set[T]) Pop() (v T, ok bool)

Example:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet[int]()
        s.Add(1)
        s.Add(2)
        s.Add(3)

        val, ok = s.Pop()

        fmt.Println(val) // 3
        fmt.Println(ok) // true
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet[int]()
        s.Add(1)
        s.Add(2)
        s.Add(3)

        val, ok = s.Pop()

        fmt.Println(val) // 3
        fmt.Println(ok) // true
    }