Skip to main content

Hash Cheatsheet

As some might know sets in python can contain only unique elements. “Similar to hash table, a hash set is also a collection of objects. In hash table, data was stored in the form of key-value pairs, whereas in hash sets, the data is stored as objects. A hash set internally uses the hash table data structure to store data items. Just like a set, a hash set also does not allow storage of duplicate elements.”

Hashing is the most common example of a space-time tradeoff. Instead of linearly searching an array every time to determine if an element is present, which takes O(n) time, we can traverse the array once and hash all the elements into a hash table. 

Basic HashSet  HashMap 
Implements  Set interface  Map interface 
Duplicates No  Yes duplicates values are allowed but no duplicate key is allowed 
Dummy values  Yes  No
Objects required during an add operation 1 2
Adding and storing mechanism  HashMap object  Hashing technique 
Speed It is comparatively slower than HashMap It is comparatively faster than HashSet because of hashing technique has been used here.
Null  Have a single null value  Single null key and any number of null values
Insertion Method Only one value is required for the insertion process. Add() function is used for insertion Two values are required for the insertion process. Put() function is used for insertion.
Data storage The data is stored as objects. The data is stored as key-value pair.
Complexity O(n)  O(1)