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. 

BasicHashSet HashMap 
Implements Set interface Map interface 
DuplicatesNo Yes duplicates values are allowed but no duplicate key is allowed 
Dummy values Yes No
Objects required during an add operation12
Adding and storing mechanism HashMap object Hashing technique 
SpeedIt is comparatively slower than HashMapIt 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 MethodOnly one value is required for the insertion process. Add() function is used for insertionTwo values are required for the insertion process. Put() function is used for insertion.
Data storageThe data is stored as objects.The data is stored as key-value pair.
ComplexityO(n) O(1)