Finished
88 * @param {Array} data
99 */
1010 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 }
1218 }
1319
1420 /**
1521 * @returns {boolean}
1622 */
1723 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;
1926 }
2027
2128 /**
2330 * @returns {boolean}
2431 */
2532 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 }
2739 }
2840
2941 /**
3143 * @returns {CustomSet}
3244 */
3345 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;
3575 }
3676
3777 /**
3979 * @returns {boolean}
4080 */
4181 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;
4397 }
4498
4599 /**
47101 * @returns {boolean}
48102 */
49103 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;
51119 }
52120
53121 /**
55123 * @returns {boolean}
56124 */
57125 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);
59129 }
60130
61131 /**
63133 * @returns {CustomSet}
64134 */
65135 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;
67155 }
68156
69157 /**
71159 * @returns {CustomSet}
72160 */
73161 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;
75178 }
76179
77180 /**
79182 * @returns {CustomSet}
80183 */
81184 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;
83201 }
84202}
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
Test NameStatus
sets with no elements are empty
Pass
sets with elements are not empty
Pass
nothing is contained in an empty set
Pass
when the element is in the set
Pass
when the element is not in the set
Pass
empty set is a subset of another empty set
Pass
empty set is a subset of non-empty set
Pass
non-empty set is not a subset of empty set
Pass
set is a subset of set with exact same elements
Pass
set is a subset of larger set with same elements
Pass
set is not a subset of set that does not contain its elements
Pass
the empty set is disjoint with itself
Pass
empty set is disjoint with non-empty set
Pass
non-empty set is disjoint with empty set
Pass
sets are not disjoint if they share an element
Pass
sets are disjoint if they share no elements
Pass
empty sets are equal
Pass
empty set is not equal to non-empty set
Pass
non-empty set is not equal to empty set
Pass
sets with the same elements are equal
Pass
sets with different elements are not equal
Pass
set is not equal to larger set with same elements
Pass
set is equal to a set constructed from an array with duplicates
Pass
add to empty set
Pass
add to non-empty set
Pass
adding an existing element does not change the set
Pass
intersection of two empty sets is an empty set
Pass
intersection of an empty set and non-empty set is an empty set
Pass
intersection of a non-empty set and an empty set is an empty set
Pass
intersection of two sets with no shared elements is an empty set
Pass
intersection of two sets with shared elements is a set of the shared elements
Pass
difference of two empty sets is an empty set
Pass
difference of empty set and non-empty set is an empty set
Pass
difference of a non-empty set and an empty set is the non-empty set
Pass
difference of two non-empty sets is a set of elements that are only in the first set
Pass
difference removes all duplicates in the first set
Pass
union of empty sets is an empty set
Pass
union of an empty set and non-empty set is the non-empty set
Pass
union of a non-empty set and empty set is the non-empty set
Pass
union of non-empty sets contains all unique elements
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.