r/datastructures • u/Designer_River_7663 • Oct 18 '26
Is anyone doing DSA with C programming ?? How to implement Hashset and Hashmaps in C programming ?
So recently inrolled in this program in which they are using C programming for DSA. But the problem with C programming is that it has no inbuilt Hashset and Hashmap Which Is sometimes crucial to use because most of the Coding platform questions uses hashsets and Hashmaps to solve the problem. So if anyone is doing DSA using C programming Can you please tell me how to implement Sets and maps in the C programming
6
Upvotes
2
u/Significant_Newt8697 Oct 18 '26
A simple google search will sort your problem::
https://www.google.com/search?q=Hashmaps+and+hashsets+in+c&oq=Hashmaps+and+hashsets+in+c&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIICAEQABgNGB4yCggCEAAYChgNGB4yCAgDEAAYFhgeMggIBBAAGBYYHjINCAUQABiGAxiABBiKBTINCAYQABiGAxiABBiKBTIHCAcQABjvBTIKCAgQABiABBiiBDIKCAkQABiABBiiBDIHCAoQIRiPAtIBCTEyNDU0ajBqN6gCFLACAQ&client=ms-android-samsung-ss&sourceid=chrome-mobile&source=chrome.ob&ie=UTF-8#lfId=ChxjMe
1
u/BetterEvidence3890 19d ago
I think it's better to switch into c++
1
u/AlarmedBag4541 Oct 18 '26
You may have to write a function with the hash map or hash set and then keep calling it maybe
1
u/Yurim Oct 18 '26
First, ask the people running the program. Maybe they want you to implement a hash table during the course of the program, or they provide a third-party library (e.g. LeetCode lets you use the uthash library), or you can build your own collection of "snippets" which can include an implementation of a hash table.
If the program is any good they will have an answer for you.
If you need to implement your own hash table and it's not part of the program you will have to choose a hash function. For small toy-like problems djb2 or sdbm (see http://www.cse.yorku.ca/~oz/hash.html) are often good enough and they are even small enough to memorize them if that becomes necessary.
Then you need the hash table itself. The simplest version is an array with a fixed size (no resizing) and "separate chaining" (i.e. each element of the array is a linked list.) Writing such a hashtable can be a valuable experience if you've never done that.
If you're a beginner and want to get your feet wet, you could start with this blog post by Ben Hoyt.
Of course lots of things can be said about hash tables. You can use open addressing with linear probing, quadratic probing, double hashing, fibonacci hashing, ... There are variants like robin hood hashing, cuckoo hashing, hash table chaining, ...
(But if you've just enrolled in a course about data structures and algorithms then you probably won't need that right away.)
If you want to see state-of-the-art hash tables I recommend this Reddit post by Jackson Allan or this blog post by Malte Skarupke.
1
u/Paul_Pedant Oct 19 '26
C (Posix) does have a library for hash table management -- see
man -s 3 hsearch. I used this for about five million objects and it behaved reasonably well. There is an extension that supports multiple concurrent hash tables.0
u/[deleted] Oct 19 '26
[deleted] — view deleted comment