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 |
public static Map<Character, Integer> countFrequency(String str) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : str.toCharArray()) {
frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
}
return frequencyMap;
}
// Example: Count letter frequency
String letters = "programming";
System.out.println(countFrequency(letters));
public static Character firstNonRepeatingChar(String str) {
Map<Character, Integer> charCount = new HashMap<>();
for (char c : str.toCharArray()) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
for (char c : str.toCharArray()) {
if (charCount.get(c) == 1) {
return c;
}
}
return null;
}
public static Character firstNonRepeatingChar(String str) {
// Create frequency map
Map<Character, Integer> charCount = new HashMap<>();
// First pass: count frequencies
for (char c : str.toCharArray()) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
// Second pass: find first char with count 1
for (char c : str.toCharArray()) {
if (charCount.get(c) == 1) {
return c;
}
}
return null;
}
System.out.println(firstNonRepeatingChar("leetcode")); // 'l'
System.out.println(firstNonRepeatingChar("aabbcc")); // null
System.out.println(firstNonRepeatingChar("aabbc")); // 'c'
public static Character firstNonRepeatingCharOptimized(String str) {
Map<Character, int[]> charInfo = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!charInfo.containsKey(c)) {
charInfo.put(c, new int[]{1, i});
} else {
charInfo.get(c)[0]++;
}
}
Character result = null;
int minIndex = str.length();
for (Map.Entry<Character, int[]> entry : charInfo.entrySet()) {
if (entry.getValue()[0] == 1 && entry.getValue()[1] < minIndex) {
result = entry.getKey();
minIndex = entry.getValue()[1];
}
}
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"]
]