Skip to content

Commit 86ff19d

Browse files
committed
Update add method to resize HashSet if load factor is exceeded
1 parent 197403b commit 86ff19d

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/dataStructures/hashSet/openAddressing/HashSet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public HashSet() {
5757
* element
5858
*/
5959
public boolean add(T element) {
60+
if (isLoadFactorExceeded()) {
61+
resize(this.capacity() * 2); // Resize to double the capacity.
62+
}
63+
6064
int bucketIndex = this.linearProbe(element);
6165
if (!this.isEmptyBucket(bucketIndex)) { // probe function returns the index of an empty bucket or the index containing the element.
6266
return false; // Duplicate elements are not added to the set.

0 commit comments

Comments
 (0)