From 160f1e413ff6262db7f80ca3e57f7eac20feac2e Mon Sep 17 00:00:00 2001
From: Madan H K <109542799+madanhk18@users.noreply.github.com>
Date: Thu, 17 Jul 2025 23:26:00 +0530
Subject: [PATCH 1/3] Add HashSet data structure implementation for integers
(#6304)
---
.../datastructures/hashset/HashSet.java | 63 +++++++++++++++++++
1 file changed, 63 insertions(+)
create mode 100644 src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
diff --git a/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java b/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
new file mode 100644
index 000000000000..7966bf47bce1
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
@@ -0,0 +1,63 @@
+
+package com.thealgorithms.datastructures.hashset;
+
+/**
+ * Example usage:
+ *
+ * HashSet set = new HashSet();
+ * set.add(5);
+ * set.add(10);
+ * System.out.println(set.contains(5)); // true
+ * set.remove(5);
+ * System.out.println(set.contains(5)); // false
+ *
+ */
+
+import java.util.LinkedList;
+
+/**
+ * A simple implementation of a HashSet for integers using separate chaining.
+ */
+public class HashSet {
+ private static final int INITIAL_CAPACITY = 16;
+ private LinkedList[] buckets;
+ private int size;
+
+ @SuppressWarnings("unchecked")
+ public HashSet() {
+ buckets = new LinkedList[INITIAL_CAPACITY];
+ for (int i = 0; i < INITIAL_CAPACITY; i++) {
+ buckets[i] = new LinkedList<>();
+ }
+ size = 0;
+ }
+
+ private int hash(int key) {
+ return Math.abs(key) % buckets.length;
+ }
+
+ public void add(int key) {
+ int idx = hash(key);
+ if (!buckets[idx].contains(key)) {
+ buckets[idx].add(key);
+ size++;
+ }
+ }
+
+ public boolean contains(int key) {
+ int idx = hash(key);
+ return buckets[idx].contains(key);
+ }
+
+ public void remove(int key) {
+ int idx = hash(key);
+ if (buckets[idx].remove((Integer) key)) {
+ size--;
+ }
+ }
+
+ public int size() {
+ return size;
+ }
+}
+
From 5368db67dac165ddd9ef6ab6c6cf0d847244ff85 Mon Sep 17 00:00:00 2001
From: Madan H K <109542799+madanhk18@users.noreply.github.com>
Date: Thu, 17 Jul 2025 23:32:55 +0530
Subject: [PATCH 2/3] Add HashSet data structure implementation for integers
(#6304) with all the Prerequisites
---
.../java/com/thealgorithms/datastructures/hashset/HashSet.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java b/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
index 7966bf47bce1..1f13a21aec1f 100644
--- a/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
+++ b/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
@@ -17,6 +17,8 @@
/**
* A simple implementation of a HashSet for integers using separate chaining.
+ *
+ * See: https://en.wikipedia.org/wiki/Hash_table
*/
public class HashSet {
private static final int INITIAL_CAPACITY = 16;
From 2f31c7dfb34f8776d5dcf1092d92bd519dc238c9 Mon Sep 17 00:00:00 2001
From: Madan H K <109542799+madanhk18@users.noreply.github.com>
Date: Thu, 17 Jul 2025 23:37:59 +0530
Subject: [PATCH 3/3] Fix raw type warning for LinkedList in HashSet
implementation
---
.../com/thealgorithms/datastructures/hashset/HashSet.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java b/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
index 1f13a21aec1f..7e97c7e56678 100644
--- a/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
+++ b/src/main/java/com/thealgorithms/datastructures/hashset/HashSet.java
@@ -27,9 +27,9 @@ public class HashSet {
@SuppressWarnings("unchecked")
public HashSet() {
- buckets = new LinkedList[INITIAL_CAPACITY];
+ buckets = (LinkedList[]) new LinkedList[INITIAL_CAPACITY];
for (int i = 0; i < INITIAL_CAPACITY; i++) {
- buckets[i] = new LinkedList<>();
+ buckets[i] = new LinkedList();
}
size = 0;
}
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy