-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathImplementSet.swift
More file actions
74 lines (63 loc) · 2.08 KB
/
ImplementSet.swift
File metadata and controls
74 lines (63 loc) · 2.08 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// S30Programs.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 12/24/25.
//
//We are implementing Set with double hashing
class ImplementSet {
//primary storage
private var storage: [[Bool]?]
//length of primary array
private var buckets: Int
//length of nested array
private var bucketItems: Int
private func hash1(key: Int) -> Int {
//primary bucket
return key % buckets
}
private func hash2(key: Int) -> Int {
//nested array bucket
return key / bucketItems
}
init() {
buckets = 1000
bucketItems = 1000
storage = Array(repeating: nil, count: buckets)
}
//primary array contains the pointer to the nested array.
//secondary array contains the items which has the same hash1 value. To identify items uniquely in secondary array, apply hash2. After hash2, you will find an index, set that index's value to true.
//O(1)
func add(key: Int) {
let bucket = hash1(key: key)
if (storage[bucket] == nil) {
//there is no nested arrey, so create a nested array
if (bucket == 0) {
storage[bucket] = Array(repeating: false, count: bucketItems + 1)
} else {
storage[bucket] = Array(repeating: false, count: bucketItems)
}
}
let bucketItem = hash2(key: key)
storage[bucket]![bucketItem] = true
}
func remove(key: Int) {
//find out the primary array bucket
let bucket = hash1(key: key)
//if there is no nested array, just return. Wouldn't remove anything
if (storage[bucket] == nil) {
return
}
let bucketItem = hash2(key: key)
storage[bucket]![bucketItem] = false
}
func contains(key: Int) -> Bool {
let bucket = hash1(key: key)
let bucketItem = hash2(key: key)
//if there is no nested array, return false
if (storage[bucket] == nil) {
return false
}
return storage[bucket]![bucketItem]
}
}