Skip to content

Commit ad1d46d

Browse files
committed
Add hash set
1 parent 8bdfcae commit ad1d46d

8 files changed

+1500
-3
lines changed
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//: Playground - noun: a place where people can play
2+
3+
public struct HashSet<T: Hashable> {
4+
private var dictionary = Dictionary<T, Bool>()
5+
6+
public mutating func insert(element: T) {
7+
dictionary[element] = true
8+
}
9+
10+
public mutating func remove(element: T) {
11+
dictionary[element] = nil
12+
}
13+
14+
public func contains(element: T) -> Bool {
15+
return dictionary[element] != nil
16+
}
17+
18+
public func allElements() -> [T] {
19+
return Array(dictionary.keys)
20+
}
21+
22+
public var count: Int {
23+
return dictionary.count
24+
}
25+
26+
public var isEmpty: Bool {
27+
return dictionary.isEmpty
28+
}
29+
}
30+
31+
var set = HashSet<String>()
32+
33+
set.insert("one")
34+
set.insert("two")
35+
set.insert("three")
36+
set.allElements()
37+
38+
set.insert("two")
39+
set.allElements()
40+
41+
set.contains("one")
42+
set.remove("one")
43+
set.allElements()
44+
set.contains("one")
45+
46+
47+
48+
/* Union */
49+
50+
extension HashSet {
51+
public func union(otherSet: HashSet<T>) -> HashSet<T> {
52+
var combined = HashSet<T>()
53+
for obj in dictionary.keys {
54+
combined.insert(obj)
55+
}
56+
for obj in otherSet.dictionary.keys {
57+
combined.insert(obj)
58+
}
59+
return combined
60+
}
61+
}
62+
63+
64+
var setA = HashSet<Int>()
65+
setA.insert(1)
66+
setA.insert(2)
67+
setA.insert(3)
68+
setA.insert(4)
69+
70+
var setB = HashSet<Int>()
71+
setB.insert(3)
72+
setB.insert(4)
73+
setB.insert(5)
74+
setB.insert(6)
75+
76+
let union = setA.union(setB)
77+
union.allElements() // [5, 6, 2, 3, 1, 4]
78+
79+
80+
81+
/* Intersection */
82+
83+
extension HashSet {
84+
public func intersect(otherSet: HashSet<T>) -> HashSet<T> {
85+
var common = HashSet<T>()
86+
for obj in dictionary.keys {
87+
if otherSet.contains(obj) {
88+
common.insert(obj)
89+
}
90+
}
91+
return common
92+
}
93+
}
94+
95+
let intersection = setA.intersect(setB)
96+
intersection.allElements() // [3, 4]
97+
98+
99+
100+
/* Difference */
101+
102+
extension HashSet {
103+
public func difference(otherSet: HashSet<T>) -> HashSet<T> {
104+
var diff = HashSet<T>()
105+
for obj in dictionary.keys {
106+
if !otherSet.contains(obj) {
107+
diff.insert(obj)
108+
}
109+
}
110+
return diff
111+
}
112+
}
113+
114+
let difference1 = setA.difference(setB)
115+
difference1.allElements() // [2, 1]
116+
117+
let difference2 = setB.difference(setA)
118+
difference2.allElements() // [5, 6]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Hash Set/HashSet.swift

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
public struct HashSet<T: Hashable> {
2+
private var dictionary = Dictionary<T, Bool>()
3+
4+
public mutating func insert(element: T) {
5+
dictionary[element] = true
6+
}
7+
8+
public mutating func remove(element: T) {
9+
dictionary[element] = nil
10+
}
11+
12+
public func contains(element: T) -> Bool {
13+
return dictionary[element] != nil
14+
}
15+
16+
public func allElements() -> [T] {
17+
return Array(dictionary.keys)
18+
}
19+
20+
public var count: Int {
21+
return dictionary.count
22+
}
23+
24+
public var isEmpty: Bool {
25+
return dictionary.isEmpty
26+
}
27+
}
28+
29+
extension HashSet {
30+
public func union(otherSet: HashSet<T>) -> HashSet<T> {
31+
var combined = HashSet<T>()
32+
for obj in dictionary.keys {
33+
combined.insert(obj)
34+
}
35+
for obj in otherSet.dictionary.keys {
36+
combined.insert(obj)
37+
}
38+
return combined
39+
}
40+
41+
public func intersect(otherSet: HashSet<T>) -> HashSet<T> {
42+
var common = HashSet<T>()
43+
for obj in dictionary.keys {
44+
if otherSet.contains(obj) {
45+
common.insert(obj)
46+
}
47+
}
48+
return common
49+
}
50+
51+
public func difference(otherSet: HashSet<T>) -> HashSet<T> {
52+
var diff = HashSet<T>()
53+
for obj in dictionary.keys {
54+
if !otherSet.contains(obj) {
55+
diff.insert(obj)
56+
}
57+
}
58+
return diff
59+
}
60+
}

0 commit comments

Comments
 (0)