Tags: #set #theory #operations #go #golang
The union of two sets is a set containing all elements that are in A or in B (possibly both). For example, {1,2}∪{2,3}
={1,2,3}
.
The intersection of two sets A and B, denoted by A∩B
, consists of all elements that are both in A and B. For example, {1,2}∩{2,3}
={2}
.
The difference (subtraction) is defined as follows. The set A−B
consists of elements that are in A but not in B. For example if A={1,2,3}
and B={3,5}
, then A−B
={1,2}
.
The complement of a set A, denoted by Ac
, is the set of all elements that are in the universal set S but are not in A.
It is easy to create a set from map:
s := map[int]bool{5: true, 2: true}
_, ok := s[6] // check for existence
s[8] = true // add element
delete(s, 2) // remove element
s_union := map[int]bool{}
for k, _ := range s1{
s_union[k] = true
}
for k, _ := range s2{
s_union[k] = true
}
s_intersection := map[int]bool{}
for k,_ := range s1 {
if s2[k] {
s_intersection[k] = true
}
}