Hashmap Examples
Our visualization tool supports hashmap operations through an intuitive graphical interface. When working with hashmaps (dictionaries in Python or objects/Maps in JavaScript), each key-value pair is represented visually, making it easy to understand the data structure's contents at a glance.
The tool dynamically tracks operations like insertions, deletions, and lookups, highlighting the affected key-value pairs as your code executes. This visual feedback is particularly useful for learning hashmap concepts or debugging hashmap-based algorithms.
Let's look at some common hashmap algorithms and their visualizations:
Frequency Counter
def count_characters(s):
char_count = {}
for char in s:
char_count[char] = char_count.get(char, 0) + 1
return char_count
# Example usage
count_characters("hello world")
def word_frequency(sentence):
words = sentence.lower().split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
word_frequency("the quick brown fox jumps over the lazy dog")
Generating interactive preview...
Two Sum Variations
def two_sum(nums, target):
num_map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_map:
return [num_map[complement], i]
num_map[num] = i
return []
# Example usage
two_sum([2, 7, 11, 15], 9)
def find_all_pairs(nums, target):
num_map = {}
pairs = []
for i, num in enumerate(nums):
complement = target - num
if complement in num_map:
for prev_index in num_map[complement]:
pairs.append([prev_index, i])
num_map.setdefault(num, []).append(i)
return pairs
find_all_pairs([1, 5, 3, 7, 2, 4, 3], 6)
Generating interactive preview...
Cache Implementation
def fibonacci(n, cache=None):
if cache is None:
cache = {}
# Base cases
if n < 2:
return n
# Check if result is in cache
if n in cache:
return cache[n]
# Calculate and store result
cache[n] = fibonacci(n-1, cache) + fibonacci(n-2, cache)
return cache[n]
# Example usage
result = fibonacci(10) # Uses memoization
def lru_cache():
cache = {}
access_order = []
capacity = 128
def get(key):
if key in cache:
# Move to most recently used
access_order.remove(key)
access_order.append(key)
return cache[key]
return None
def put(key, value):
if key in cache:
# Update existing key
access_order.remove(key)
elif len(cache) >= capacity:
# Remove least recently used
lru_key = access_order.pop(0)
del cache[lru_key]
cache[key] = value
access_order.append(key)
return {"get": get, "put": put}
# Example usage
cache = lru_cache()
cache["put"](1, 1) # adds 1
cache["put"](2, 2) # adds 2
cache["put"](3, 3) # adds 3
Generating interactive preview...
Set Operations with Hashmaps
def find_intersection(nums1, nums2):
# Convert first array to hashmap for counting
count = {}
for num in nums1:
count[num] = count.get(num, 0) + 1
# Find intersection
result = []
for num in nums2:
if num in count and count[num] > 0:
result.append(num)
count[num] -= 1
return result
find_intersection([1, 2, 2, 1], [2, 2])
def group_anagrams(words):
groups = {}
for word in words:
# Sort characters to create key
key = ''.join(sorted(word))
groups.setdefault(key, []).append(word)
return list(groups.values())
group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
Generating interactive preview...