Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Go to Problems

# Hashing Implementation Details

##### C

C has nothing native for hashmap We need to write one ourselves.

##### C++ :

C++ has unordered_map which is implemented via hashing. We will discuss treemaps in C++ as well later, which in practice seem to behave faster in C++.

###### Declaration
``````    unordered_map<int, int> A; // declares an empty map. O(1)
``````
###### Adding an key, value pair :
``````    A.insert({key, value}); // O(1) time on average
``````
###### Find the value for key = K:
``````    if (A.find(K) == A.end()) return null; //  means that K does not exist in A.
else return A[K];    // O(1) average case. Rare worst case O(n).
``````
###### Size ( number of elements ) of the vector :
``````    A.size()  // O(1)
``````
###### Erase from the map :
``````    if (A.find(K) != A.end()) A.erase(A.find(K));
OR A.erase(K);
``````
##### JAVA :

Now we come to one of the most popular data structures in Java, HashMap.

###### Declaration :
``````    HashMap<Integer, Integer> A = new HashMap<Integer, Integer>(); // declares an empty map.
``````
###### Adding an key, value pair :
``````    A.put(key, value); // O(1) time on average
``````
###### Find the value for key = K:
``````    A.get(K) // null if the key K is not present
A.containsKey(K) tells if the key K is present.
// Both operations O(1) average time. O(n) rare worst case
``````
###### Size ( number of elements ) of the vector :
``````    A.size()  // O(1)
``````
###### Erase from the map :
``````    A.remove(K);
``````
##### PYTHON

Python has dictionaries which serve the same purpose.

###### Declaration :
``````    A = {}
``````
###### Adding an key, value pair :
``````    A[key] = value // O(1) average time. O(n) worst case
``````
###### Find the value for key = K:
``````    A[K] // O(1) average, O(n) worst
``````
###### Size ( number of elements ) of the vector :
``````    len(A) // O(1)
``````
###### Erase from the map :
``````        del A[K] // O(1) average, O(n) worst
``````

## Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

## Hashing Problems

Hash search
Problem Score Companies Time Status
Colorful Number 150 44:55
Largest Continuous Sequence Zero Sum 200
68:45
Longest Subarray Length 200 54:34
First Repeating element 200 21:17
2 Sum 300 49:18
4 Sum 325 72:04
Valid Sudoku 325 50:31
Diffk II 375
29:06
Key formation
Problem Score Companies Time Status
Pairs With Given Xor 200 26:35
Anagrams 350 46:36
Equal 350
71:07
Copy List 450 55:59
Maths and hashing
Problem Score Companies Time Status
Check Palindrome! 200 18:00
Fraction 450 81:35
Points on the Straight Line 450 78:18
Incremental hash
Problem Score Companies Time Status
An Increment Problem 200 49:12
Subarray with given XOR 200 54:12
Two out of Three 200 33:13
Substring Concatenation 1000
71:15
Hashing two pointer
Problem Score Companies Time Status
Subarray with B odd numbers 200 50:51
Window String 350 81:47
Longest Substring Without Repeat 350 51:12
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *