Today Study

  • Intersection = 교집합
  • Union = 합집합
  • Subtracting = 차집합
  • SymmetricDifference = 교집합의 여집합

Intersection = 교집합

var mostValuable: Set<String> = ["Money", "Power", "Royalty"]
var mostValuableToMe = Set(["Love", "Family", "FriendShip", "Mind"])

if mostValuable.isDisjoint(with: mostValuableToMe) {
    print("mostValuable and mostValueableToMe = ∅")
} else {
    print("mostValuable and mostValuableToMe")
}
  • .isDisjoint(with:)
    • Summary : Returns a Boolean value that indicates whether this set has no members in common with the given set
    • Declaration : func isDisjoint(with other: Set) -> Bool
    • Parameters : others - Another Set
    • Returns : true if the set has no elements in common with other, otherwise, false
let commonSet = mostValuable.intersection(mostValuableToMe)
commonSet // 결과값 = Set([])
  • mostValuable 집합과 mostValuableToMe 집합의 교집합이 없기 때문에 결과값은 빈 Set 값이 된다
  • .intersection()
    • Summary : Returns a new set with the elements that are common to both this set and the given sequence.
    • Declaration : func intersection(_ other: Set) -> Set
    • Parameters : other - Another set.
    • Returns : A new set.
mostValuableToMe.formIntersection(mostValuable)
mostValuableToMe
  • .formIntersection()
    • Summary : Removes the elements of the set that aren’t also in the gince sequence
    • Declaraction : mutating func formIntersection(_ other: S) where String == S.Element, S: Sequence
    • Parameters : other - A sequence of elements. other must be finite.
    • Discussion
      • In the following example, the elements of the employees set that are not also members of the neighbors set are removed. In parricular, the names “Alicia”, “Chris” and “Diana” are removed.
  • example
var employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors = ["Bethany", "Eric", "Forlani", "Greta"]

employees.formIntersection(neighbors)
print(employees) // 결과값 : ["Bethany", "Eric"]
print(neighbors) // 결과값 : ["Bethany", "Eric", "Forlani", "Greta"]

Union = 합집합

var exampleUnionSet = Set(["Appel", "Orange", "Melon", "Kiwi"])
var exampleFruitSet = Set(["Banana", "Papaya", "Kiwi", "Pineapple"])

var unionExample = exampleUnionSet.union(exampleFruitSet)
unionExample
print(unionExample)
  • .union()
    • Summary : Returns a new with the elements of both this set and the given sequence.
    • Declaration : func union(_ other: S) -> Set where String == S.Element, S : Sequence
    • Discussion
      • In the following example, the attendeesAndVisitors set is made up of the elements of the attendees set and the visitors array
let attendees: Set = ["Alicia", "Bethany", "Diana"]
let visitors = ["Marcia", "Nathaniel"]
let attendeesAndVisitors = attendees.union(visitors)
print(attendeesAndVisitors) 
// Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]"
  • if the set already contains one or more elements that are also in other, the existing members are kept. If other contains multiple instances of equivalent elements, only the first instance is kept.
let initialIndices = Set(0..<5)
let expandedIndices = initialIndices.union([2, 3, 6, 6, 7, 7])
print(expandedIndices)
// Prints "[2, 4, 6, 7, 0, 1, 3]"
  • Parameters : other - A sequence of elements. other must be finite
  • Returns : A new set with the unique elements of this set and other
unionExample = exampleUnionSet // ["Appel", "Orange", "Melon", "Kiwi"]
unionExample.formUnion(exampleFruitSet)
unionExample // ["Banana", "Papaya", "Kiwi", "Pineapple", "Appel", "Orange", "Melon"]
  • .formUnion()
    • Summary : Inserts the elements of the given sequence into the set.
    • Declaration : mutating func formUnion(_ other: S) where String == S.Element, S : Sequence
    • Discussion : if the set already contains one or more elements that are also in other, the exisiting members are kept. if other contains multiple instances of equivalent elements, only the first instance is kept.
    • Parameters : other - A sequence of elements. other must be finite.
  • Subtracting = 차집합
var favoriteFruits = Set(["Apple", "Orange", "Melon", "Kiwi"])
var tropicalFruits = Set(["Banana", "Papaya", "Kiwi", "Pineapple"])

let exampleIntersectionSet = favoriteFruits.subtracting(tropicalFruits)
exampleIntersectionSet
  • subtracting()
    • Summary : Returns a new set containing the elements of this set that do not occur in the given set.
    • Declaration : func subtractimg(_ other: Set) -> Set
    • Discussion : in the following example, the nonNeighbors set is made up of the elements of the employees1 set that are not elements of neighbors1
let employees1: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors1: Set = ["Bethany", "Eric", "Forlani", "Greta"]
let nonNeighbors = employees1.subtracting(neighbors1)
print(nonNeighbors)
// Prints "["Diana", "Chris", "Alicia"]"

favoriteFruits.subtract(tropicalFruits)
favoriteFruits
  • subtract()
    • Summary : Removes the elements of the given set from this set
    • Declaration : mutating func subtract(_ other: Set)
    • Discussion : in the following example, the elements of the employsees2 set that are also members of the neighbors2 set are removed. in particular, the names “Bethany” and “Eric” are removed from employees2
    • Parameters : other - Another set
var employees2: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors2: Set = ["Bethany", "Eric", "Forlani", "Greta"]
employees2.subtract(neighbors2)
print(employees2)
// Prints "["Diana", "Chris", "Alicia"]"
  • SymmetricDifference = 교집합의 여집합
var exampleNameList = Set(["Jun", "Sua", "SoYeon", "DaYoon"])
var exampleAnotherName: Set<String> = ["Greace","Chris","Alicia","Jun"]

let exclusiveSet = exampleNameList.symmetricDifference(exampleAnotherName)
exclusiveSet // {"Alicia", "Sua", "Chris", "DaYoon", "SoYeon", "Greace"}

exampleNameList.formSymmetricDifference(exampleAnotherName)
exampleNameList //{"Alicia", "Sua", "Chris", "DaYoon", "SoYeon", "Greace"}
  • symmetricDifference
    • Summary : Returns a new with the elements that either in this set or in the given sequence, but not in both
    • Declaration : func symmtricDifference(_ other: S) -> Set where String == S.Element, S : Sequence
    • Declaration : In the follewing example, the eitherNeighborsOrEmployees set is made up of the elements of the employees3 and neighbors3 sets that are not in both employees3 and neighbors3. In particular, the names “Bethany” and “Eric” do not appear in eitherNeighborsOrEmployees.
    • Parameters : other - A sequence of elements. other must be finite.
    • Returns : A new set.
let employees3: Set = ["Alicia", "Bethany", "Diana", "Eric"]
let neighbors3 = ["Bethany", "Eric", "Forlani"]
let eitherNeighborsOrEmployees = employees3.symmetricDifference(neighbors3)
print(eitherNeighborsOrEmployees)
// Prints "["Diana", "Forlani", "Alicia"]"
  • formSymmetricDifference
    • Summary : Removes the elements of the set that are also in the given sequence and adds the members of the sequence that are not already in the set.
    • Declaration : mutating func formSymmetricDifference(_ other: Set)
    • Discussion : In the following example, the elements of the employees4 set that are also members of neighbors4 are removed from employees4, while the elements of neighbors4 that are not members of employees4 are added to employees4. In particular, the names “Alicia”, “Chris”, and “Diana” are removed from employees4 while the names “Forlani” and “Greta” are added
var employees4: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors4: Set = ["Bethany", "Eric", "Forlani", "Greta"]
employees4.formSymmetricDifference(neighbors4)
print(employees4)
// Prints "["Diana", "Chris", "Forlani", "Alicia", "Greta"]"