C has nothing native for hashmap We need to write one ourselves.
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++.
unordered_map<int, int> A; // declares an empty map. O(1)
A.insert({key, value}); // O(1) time on average
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).
A.size() // O(1)
if (A.find(K) != A.end()) A.erase(A.find(K));
OR A.erase(K);
Now we come to one of the most popular data structures in Java, HashMap.
HashMap<Integer, Integer> A = new HashMap<Integer, Integer>(); // declares an empty map.
A.put(key, value); // O(1) time on average
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
A.size() // O(1)
A.remove(K);
Python has dictionaries which serve the same purpose.
A = {}
A[key] = value // O(1) average time. O(n) worst case
A[K] // O(1) average, O(n) worst
len(A) // O(1)
del A[K] // O(1) average, O(n) worst
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Colorful Number | 150 |
|
44:55 | |
Largest Continuous Sequence Zero Sum | 200 |
|
68:45 | |
Longest Subarray Length | 200 |
|
57:25 | |
First Repeating element | 200 |
|
21:50 | |
2 Sum | 300 |
|
49:18 | |
4 Sum | 325 |
|
72:27 | |
Valid Sudoku | 325 |
|
50:42 | |
Diffk II | 375 |
|
29:06 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Pairs With Given Xor | 200 |
|
27:20 | |
Anagrams | 350 |
|
46:36 | |
Equal | 350 |
|
71:23 | |
Copy List | 450 |
|
56:18 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Check Palindrome! | 200 |
|
18:55 | |
Fraction | 450 |
|
81:53 | |
Points on the Straight Line | 450 |
|
78:52 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
An Increment Problem | 200 |
|
50:48 | |
Subarray with given XOR | 200 |
|
56:12 | |
Two out of Three | 200 |
|
33:54 | |
Substring Concatenation | 1000 |
|
71:27 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Subarray with B odd numbers | 200 |
|
52:45 | |
Window String | 350 |
|
82:16 | |
Longest Substring Without Repeat | 350 |
|
51:12 |