Master advanced hash table techniques and learn how to solve complex problems efficiently using hash-based data structures.
Track occurrences of elements in arrays, strings, or streams of data.
Store frequently accessed data for quick retrieval.
Efficiently remove or identify duplicate items.
Data Structure | Main Features | When to Use | Performance |
---|---|---|---|
Object |
|
Simple key-value mapping with string keys | Average O(1) for access, insertion, deletion |
Map |
|
When key order matters or when using non-string keys | Average O(1) for access, insertion, deletion |
Set |
|
When you need to track unique values without associated keys | Average O(1) for access, insertion, deletion |
WeakMap / WeakSet |
|
When tracking object references that should be garbage collected | Average O(1) for access, insertion, deletion |
function countFrequency(arr) {
const frequencyMap = new Map();
for (const item of arr) {
frequencyMap.set(item, (frequencyMap.get(item) || 0) + 1);
}
return frequencyMap;
}
// Example: Count letter frequency
const letters = "programming";
function firstNonRepeatingChar(str) {
// Create frequency map
const charCount = new Map();
// First pass: count frequencies
for (const char of str) {
charCount.set(char, (charCount.get(char) || 0) + 1);
}
// Second pass: find first char with count 1
for (const char of str) {
if (charCount.get(char) === 1) {
return char;
}
}
return null;
}
function firstNonRepeatingChar(str) {
// Create frequency map
const charCount = new Map();
// First pass: count frequencies
for (const char of str) {
charCount.set(char, (charCount.get(char) || 0) + 1);
}
// Second pass: find first char with count 1
for (const char of str) {
if (charCount.get(char) === 1) {
return char;
}
}
return null;
}
console.log(firstNonRepeatingChar("leetcode")); // "l"
console.log(firstNonRepeatingChar("aabbcc")); // null
console.log(firstNonRepeatingChar("aabbc")); // "c"
function firstNonRepeatingCharOptimized(str) {
const charInfo = new Map();
// Single pass: track both count and first position
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (!charInfo.has(char)) {
charInfo.set(char, { count: 1, firstIndex: i });
} else {
const info = charInfo.get(char);
info.count++;
}
}
// Find character with count 1 and minimum index
let result = null;
let minIndex = str.length;
for (const [char, info] of charInfo) {
if (info.count === 1 && info.firstIndex < minIndex) {
result = char;
minIndex = info.firstIndex;
}
}
return result;
}
Implement a function that finds the first character that repeats in a string.
firstRepeatingChar("abcdeef") → "e"
firstRepeatingChar("abcde") → null
firstRepeatingChar("abba") → "b"
Create a function that returns a map of character frequencies in descending order.
charFrequency("programming") →
{
"g": 2,
"r": 2,
"m": 2,
"p": 1,
"o": 1,
"a": 1,
"i": 1,
"n": 1
}
Write a function that groups anagrams together from an array of strings.
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]) →
[
["eat", "tea", "ate"],
["tan", "nat"],
["bat"]
]