You are on page 1of 23

Hashing

Presented by:
Mumal Rathore
Computer Club
What is Hashing?
Hashing in the data structure is a technique of mapping a large chunk
of data into small tables using a hashing function. It is a technique that
uniquely identifies a specific item from a collection of similar items.
Hash Table and Hash Function
A hash table is a generalization of the simpler notion of an ordinary array. Direct addressing
is applicable when we can afford to allocate an array that has one position for every possible key.

When the number of keys actually stored is small relative to the total number of possible keys, hash
tables become an effective alternative to directly addressing an array, since a hash table typically uses
an array of size proportional to the number of keys actually stored.

Instead of using the key as an array index directly, the array index is computed from the keys using
hash functions.

The "bottom line" is that hashing is an extremely effective and practical technique.
Why Hashing?

Direct Addressing Hashing


Collision

The fly in the ointment of this beautiful idea is that two keys may hash to the
same slot--a collision. Fortunately, there are effective techniques for resolving
the conflict created by collisions.
• Chaining

• Open Addressing:
Chaining
• In chaining, we put all
the elements that hash to
the same slot in a linked
list. Slot j contains a
pointer to the head of the
list of all stored elements
that hash to j; if there are
no such elements, slot j
contains NIL.
• CHAINED-HASH-INSERT(T, x)
insert x at the head of list T[h(key[x])]
• CHAINED-HASH-SEARCH(T, k)
search for an element with key k in list
T[h(k)]
• CHAINED-HASH-DELETE(T, x)
delete x from the list T[h(key[x])]
Open Addressing

In open addressing, all elements are stored in the hash


table itself.
To perform insertion using open addressing, we
successively examine, or probe, the hash table until we find
an empty slot in which to put the key.
Applications
Java
HashMap
• Declaration :
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();

• Insertion: Adding an key, value pair :


hash.put(key, value); // O(1) time on average
• Searching: Find the value for key = K:
int x = hash.get(K) ;
boolean flag = hash.containsKey(K);

• Deletion: Erase key K from the map :


hash.remove(K);

• Size ( number of elements ) of the map:


int length = hash.size() ;
Documentation:
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
Storing Frequency using HashMap
• for(int i = 0; i < N; i++){
if( !hash.containsKey(arr[i]) )
hash.put(arr[i], 1);
else
hash.put(arr[i], hash.get(arr[i])+1);
}
Traversing
Set<Integer> set = hash.keySet();
for(int e : set){
System.out.println(hash.get(e));
}
Find whether an array is a
subset of another array.

• Given two arrays: arr1[0..m-1] and


arr2[0..n-1]. Find whether arr2[] is a
subset of arr1[] or not. Both the arrays
are not in sorted order. It may be
assumed that elements in both array are
distinct.
Find a pair with given sum.

Given an array A[] of n


numbers and another
number x, determines
whether or not there exist
two elements in A[] whose
sum is exactly x.
Check whether a subarray with sum 0 exists or
not.
• Given an unsorted array
of nonnegative integers,
find a continuous subarray
which adds to a given
number.
Anagrams
• Given an array of strings, return all
groups of strings that are anagrams.
• Input:
• N=5
• words[] = {act, god ,cat ,dog ,tac}
• Output:
• god dog
• act cat tac
Longest Consecutive
Subsequence
• Given an array of integers, find the
length of the longest sub-sequence
such that elements in the
subsequence are consecutive
integers, the consecutive numbers
can be in any order.
Extra Resources/

Where to use hashing? : https://www.geeksforgeeks.org/advantages-of-bst-over-hash-table/

Practice Questions:
GeeksforGeeks

LeetCode
Bibliography
• Introduction to Algorithms – THOMAS H. CORMEN, CHARLES E.
RONALD L. CLIFFORD STEINRIVESTLEISERSON
http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap12.htm
• https://www.geeksforgeeks.org/
Thank You!
LinkedIn | Computer Club

You might also like