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 |
|
58:07 | |
| First Repeating element | 200 |
|
22:04 | |
| 2 Sum | 300 |
|
49:18 | |
| 4 Sum | 325 |
|
72:27 | |
| Valid Sudoku | 325 |
|
50:45 | |
| Diffk II | 375 |
|
29:06 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| Pairs With Given Xor | 200 |
|
27:31 | |
| Anagrams | 350 |
|
46:36 | |
| Equal | 350 |
|
71:27 | |
| Copy List | 450 |
|
56:25 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| Check Palindrome! | 200 |
|
19:17 | |
| Fraction | 450 |
|
81:56 | |
| Points on the Straight Line | 450 |
|
79:03 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| An Increment Problem | 200 |
|
51:16 | |
| Subarray with given XOR | 200 |
|
56:12 | |
| Two out of Three | 200 |
|
34:00 | |
| Substring Concatenation | 1000 |
|
71:30 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| Subarray with B odd numbers | 200 |
|
53:15 | |
| Window String | 350 |
|
82:25 | |
| Longest Substring Without Repeat | 350 |
|
51:12 |