Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
my_hash_set = [ [None], ['Jones'], [None], ['Lisa'], [None], ['Bob'], [None], ['Siri'], ['Pete'], [None] ] def hash_function(value): return sum(ord(char) for char in value) % 10 def add(value): index = hash_function(value) bucket = my_hash_set[index] if value not in bucket: bucket.append(value) def contains(value): index = hash_function(value) bucket = my_hash_set[index] return value in bucket add('Stuart') print(my_hash_set) print('Contains Stuart:',contains('Stuart')) #Python
#include
#include
#include
#define HASH_SET_SIZE 10 typedef struct Node { char* value; struct Node* next; } Node; Node* myHashSet[HASH_SET_SIZE] = {NULL}; int hashFunction(const char* value) { int sum = 0; for (int i = 0; value[i] != '\0'; i++) { sum += value[i]; } return sum % HASH_SET_SIZE; } void add(const char* value) { int index = hashFunction(value); Node* newNode = (Node*)malloc(sizeof(Node)); // Manually allocate memory and copy the string newNode->value = (char*)malloc(strlen(value) + 1); // +1 for the null terminator strcpy(newNode->value, value); newNode->next = NULL; if (myHashSet[index] == NULL) { myHashSet[index] = newNode; } else { Node* current = myHashSet[index]; while (current->next != NULL) { current = current->next; } current->next = newNode; } } int contains(const char* value) { int index = hashFunction(value); Node* current = myHashSet[index]; while (current != NULL) { if (strcmp(current->value, value) == 0) { return 1; // True } current = current->next; } return 0; // False } void freeHashSet() { for (int i = 0; i < HASH_SET_SIZE; i++) { Node* current = myHashSet[i]; while (current != NULL) { Node* temp = current; current = current->next; free(temp->value); // Free the manually allocated string memory free(temp); } } } int main() { add("Jones"); add("Lisa"); add("Bob"); add("Siri"); add("Pete"); add("Stuart"); for (int i = 0; i < HASH_SET_SIZE; i++) { printf("Bucket %d: ", i); Node* current = myHashSet[i]; while (current != NULL) { printf("%s -> ", current->value); current = current->next; } printf("NULL\n"); } printf("\nContains Stuart: %s\n", contains("Stuart") ? "true" : "false"); freeHashSet(); // Cleanup allocated memory return 0; } // C
import java.util.LinkedList; public class Main { static LinkedList
[] myHashSet = new LinkedList[10]; public static void main(String[] args) { for (int i = 0; i < myHashSet.length; i++) { myHashSet[i] = new LinkedList<>(); } add("Jones"); add("Lisa"); add("Bob"); add("Siri"); add("Pete"); add("Stuart"); for (LinkedList
bucket : myHashSet) { System.out.println(bucket); } System.out.println("Contains Stuart: " + contains("Stuart")); } public static int hashFunction(String value) { int sum = 0; for (char c : value.toCharArray()) { sum += c; } return sum % 10; } public static void add(String value) { int index = hashFunction(value); LinkedList
bucket = myHashSet[index]; if (!bucket.contains(value)) { bucket.add(value); } } public static boolean contains(String value) { int index = hashFunction(value); LinkedList
bucket = myHashSet[index]; return bucket.contains(value); } } //Java
Python result:
C result:
Java result:
[[None], ['Jones'], [None], ['Lisa', 'Stuart'], [None], ['Bob'], [None], ['Siri'], ['Pete'], [None]]
Contains Stuart: True
Bucket 0: NULL
Bucket 1: Jones -> NULL
Bucket 2: NULL
Bucket 3: Lisa -> Stuart -> NULL
Bucket 4: NULL
Bucket 5: Bob -> NULL
Bucket 6: NULL
Bucket 7: Siri -> NULL
Bucket 8: Pete -> NULL
Bucket 9: NULL
Contains Stuart: true
[]
[Jones]
[]
[Lisa, Stuart]
[]
[Bob]
[]
[Siri]
[Pete]
[]
Contains Stuart: true