forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
46 lines (33 loc) · 853 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//: Playground - noun: a place where people can play
var set = HashSet<String>()
set.insert("one")
set.insert("two")
set.insert("three")
set.allElements()
set.insert("two")
set.allElements()
set.contains("one")
set.remove("one")
set.allElements()
set.contains("one")
/* Union */
var setA = HashSet<Int>()
setA.insert(1)
setA.insert(2)
setA.insert(3)
setA.insert(4)
var setB = HashSet<Int>()
setB.insert(3)
setB.insert(4)
setB.insert(5)
setB.insert(6)
let union = setA.union(setB)
union.allElements() // [5, 6, 2, 3, 1, 4]
/* Intersection */
let intersection = setA.intersect(setB)
intersection.allElements() // [3, 4]
/* Difference */
let difference1 = setA.difference(setB)
difference1.allElements() // [2, 1]
let difference2 = setB.difference(setA)
difference2.allElements() // [5, 6]