| 8 | 8 | * @param {Array} data |
| 9 | 9 | */ |
| 10 | 10 | constructor(data) { |
| 11 | throw new Error('Remove this line and implement the function'); | |
| 11 | this._elements = new Map(); | |
| 12 | this._primitiveElements = {}; | |
| 13 | if (data) { | |
| 14 | for (const el of data) { | |
| 15 | this.add(el); | |
| 16 | } | |
| 17 | } | |
| 12 | 18 | } |
| 13 | 19 | |
| 14 | 20 | /** |
| 15 | 21 | * @returns {boolean} |
| 16 | 22 | */ |
| 17 | 23 | empty() { |
| 18 | throw new Error('Remove this line and implement the function'); | |
| 24 | // Edge Case: Empty set | |
| 25 | return this._elements.size === 0 && Object.keys(this._primitiveElements).length === 0; | |
| 19 | 26 | } |
| 20 | 27 | |
| 21 | 28 | /** |
| 23 | 30 | * @returns {boolean} |
| 24 | 31 | */ |
| 25 | 32 | contains(el) { |
| 26 | throw new Error('Remove this line and implement the function'); | |
| 33 | // Edge Case: Check for element in empty set | |
| 34 | if (el === null || el === undefined || typeof el !== 'object') { | |
| 35 | return this._primitiveElements[el] === true; | |
| 36 | } else { | |
| 37 | return this._elements.has(el); | |
| 38 | } | |
| 27 | 39 | } |
| 28 | 40 | |
| 29 | 41 | /** |
| 31 | 43 | * @returns {CustomSet} |
| 32 | 44 | */ |
| 33 | 45 | add(el) { |
| 34 | throw new Error('Remove this line and implement the function'); | |
| 46 | // Edge Case: Adding duplicate elements | |
| 47 | // Edge Case: Adding null or undefined values | |
| 48 | if (el === null || el === undefined || typeof el !== 'object') { | |
| 49 | this._primitiveElements[el] = true; | |
| 50 | } else { | |
| 51 | this._elements.set(el, true); | |
| 52 | } | |
| 53 | return this; | |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * @param {*} el | |
| 58 | * @returns {CustomSet} | |
| 59 | */ | |
| 60 | delete(el) { | |
| 61 | // Edge Case: Deleting non-existent element | |
| 62 | if (el === null || el === undefined || typeof el !== 'object') { | |
| 63 | delete this._primitiveElements[el]; | |
| 64 | } else { | |
| 65 | this._elements.delete(el); | |
| 66 | } | |
| 67 | return this; | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * @returns {number} | |
| 72 | */ | |
| 73 | size() { | |
| 74 | return this._elements.size + Object.keys(this._primitiveElements).length; | |
| 35 | 75 | } |
| 36 | 76 | |
| 37 | 77 | /** |
| 39 | 79 | * @returns {boolean} |
| 40 | 80 | */ |
| 41 | 81 | subset(other) { |
| 42 | throw new Error('Remove this line and implement the function'); | |
| 82 | // Edge Case: Empty set is subset of any set | |
| 83 | // Edge Case: Comparing with empty set | |
| 84 | // Check primitive elements | |
| 85 | for (const el in this._primitiveElements) { | |
| 86 | if (!other.contains(el)) { | |
| 87 | return false; | |
| 88 | } | |
| 89 | } | |
| 90 | // Check object elements | |
| 91 | for (const el of this._elements.keys()) { | |
| 92 | if (!other.contains(el)) { | |
| 93 | return false; | |
| 94 | } | |
| 95 | } | |
| 96 | return true; | |
| 43 | 97 | } |
| 44 | 98 | |
| 45 | 99 | /** |
| 47 | 101 | * @returns {boolean} |
| 48 | 102 | */ |
| 49 | 103 | disjoint(other) { |
| 50 | throw new Error('Remove this line and implement the function'); | |
| 104 | // Edge Case: Two empty sets are disjoint | |
| 105 | // Edge Case: One empty set and one non-empty set | |
| 106 | // Check primitive elements | |
| 107 | for (const el in this._primitiveElements) { | |
| 108 | if (other.contains(el)) { | |
| 109 | return false; | |
| 110 | } | |
| 111 | } | |
| 112 | // Check object elements | |
| 113 | for (const el of this._elements.keys()) { | |
| 114 | if (other.contains(el)) { | |
| 115 | return false; | |
| 116 | } | |
| 117 | } | |
| 118 | return true; | |
| 51 | 119 | } |
| 52 | 120 | |
| 53 | 121 | /** |
| 55 | 123 | * @returns {boolean} |
| 56 | 124 | */ |
| 57 | 125 | eql(other) { |
| 58 | throw new Error('Remove this line and implement the function'); | |
| 126 | // Edge Case: Two empty sets are equal | |
| 127 | // Edge Case: Sets of different sizes | |
| 128 | return this.subset(other) && other.subset(this); | |
| 59 | 129 | } |
| 60 | 130 | |
| 61 | 131 | /** |
| 63 | 133 | * @returns {CustomSet} |
| 64 | 134 | */ |
| 65 | 135 | union(other) { |
| 66 | throw new Error('Remove this line and implement the function'); | |
| 136 | // Edge Case: Union with empty set | |
| 137 | const result = new CustomSet(); | |
| 138 | // Add primitive elements from this set | |
| 139 | for (const el in this._primitiveElements) { | |
| 140 | result.add(el); | |
| 141 | } | |
| 142 | // Add object elements from this set | |
| 143 | for (const el of this._elements.keys()) { | |
| 144 | result.add(el); | |
| 145 | } | |
| 146 | // Add primitive elements from other set | |
| 147 | for (const el in other._primitiveElements) { | |
| 148 | result.add(el); | |
| 149 | } | |
| 150 | // Add object elements from other set | |
| 151 | for (const el of other._elements.keys()) { | |
| 152 | result.add(el); | |
| 153 | } | |
| 154 | return result; | |
| 67 | 155 | } |
| 68 | 156 | |
| 69 | 157 | /** |
| 71 | 159 | * @returns {CustomSet} |
| 72 | 160 | */ |
| 73 | 161 | intersection(other) { |
| 74 | throw new Error('Remove this line and implement the function'); | |
| 162 | // Edge Case: Intersection with empty set results in empty set | |
| 163 | // Edge Case: Intersection of two empty sets | |
| 164 | const result = new CustomSet(); | |
| 165 | // Check primitive elements | |
| 166 | for (const el in this._primitiveElements) { | |
| 167 | if (other.contains(el)) { | |
| 168 | result.add(el); | |
| 169 | } | |
| 170 | } | |
| 171 | // Check object elements | |
| 172 | for (const el of this._elements.keys()) { | |
| 173 | if (other.contains(el)) { | |
| 174 | result.add(el); | |
| 175 | } | |
| 176 | } | |
| 177 | return result; | |
| 75 | 178 | } |
| 76 | 179 | |
| 77 | 180 | /** |
| 79 | 182 | * @returns {CustomSet} |
| 80 | 183 | */ |
| 81 | 184 | difference(other) { |
| 82 | throw new Error('Remove this line and implement the function'); | |
| 185 | // Edge Case: Difference with empty set is the original set | |
| 186 | // Edge Case: Difference of empty set with any set is empty set | |
| 187 | const result = new CustomSet(); | |
| 188 | // Check primitive elements | |
| 189 | for (const el in this._primitiveElements) { | |
| 190 | if (!other.contains(el)) { | |
| 191 | result.add(el); | |
| 192 | } | |
| 193 | } | |
| 194 | // Check object elements | |
| 195 | for (const el of this._elements.keys()) { | |
| 196 | if (!other.contains(el)) { | |
| 197 | result.add(el); | |
| 198 | } | |
| 199 | } | |
| 200 | return result; | |
| 83 | 201 | } |
| 84 | 202 | } |
| 203 | ||
| 204 | // Handled Edge Cases: Empty set operations, Duplicate element insertion, Null or undefined values, Set comparison with different sizes, Intersection/union with empty sets |