Check If N and Its Double Exist — Binary Search or Hash Set
Check if any element and its double both exist in an array using a hash set or sorting with binary search.
webcoderspeed.com
50 articles
Check if any element and its double both exist in an array using a hash set or sorting with binary search.
Master HashMap, HashSet, and cache design patterns with 45 problems, 5-language solutions, and MAANG company tags.
Find two indices that add to target in O(n) by storing each number in a HashMap and looking up the complement.
Check if two strings are anagrams by comparing their character frequency counts using an array or HashMap.
Check if a ransom note can be constructed from magazine letters using a character frequency map.
Check if two strings are isomorphic by maintaining bidirectional character mappings to ensure a consistent one-to-one correspondence.
Check if a string follows a given pattern using bidirectional mapping between pattern chars and words.
Determine if a number is happy by repeatedly summing digit squares and detecting cycles using a HashSet or Floyd algorithm.
Find if any two duplicate elements are within k indices of each other using a HashMap of last-seen positions.
Find characters common to all strings in a list by intersecting their frequency arrays with element-wise minimum.
Count how many stones are jewels by storing jewel types in a HashSet and checking each stone.
Find all groups of duplicate files by parsing path strings and grouping files by their content using a HashMap.
Group strings that are anagrams together by using their sorted form as a HashMap key.
Find the k most frequent elements in O(n) using bucket sort by frequency instead of a heap.
Design a Least Recently Used cache with O(1) get and put operations using a HashMap combined with a doubly linked list.
Count subarrays with sum exactly k using prefix sums stored in a HashMap to find valid starting positions in O(n).
Check if a continuous subarray of length >= 2 sums to a multiple of k using prefix sum modulo k with a HashMap.
Find the longest consecutive integer sequence in O(n) by using a HashSet and only starting sequences from their minimum element.
Design a set with O(1) insert, delete, and getRandom using a dynamic array combined with a HashMap for index tracking.
Find all starting indices of anagram substrings by using a fixed window with character frequency comparison.
Implement weighted random selection by building a prefix sum array and using binary search to find the sampled index.
Find the vertical line that crosses the fewest bricks by counting the most frequent gap position using a HashMap.
Check if all value frequencies are unique using two hash sets in O(n).
Count substrings with at most one odd-frequency letter using bitmask prefix XOR and a hash map.
Count (row, col) pairs that are equal by hashing each row tuple and each column tuple.
Find if a BST contains two nodes summing to target using a hash set during DFS traversal.
Find the longest arithmetic subsequence using dp[i][diff] = length ending at index i with given difference.
Count pairs (i,j) where j-i != nums[j]-nums[i] by counting good pairs via frequency map of nums[i]-i.
Find the smallest subarray to remove so the remaining sum is divisible by p using prefix modular arithmetic.
Count tuples (a,b,c,d) where a*b=c*d by counting pairs with each product using a frequency map.
Design a URL shortener using two hash maps for O(1) encode and decode operations.
Convert a fraction to decimal string with recurring part detected via remainder position tracking.
Find all duplicate subtrees by serializing each subtree and tracking serialization counts in a hash map.
Implement a hash map from scratch using an array of buckets with chaining for collision resolution.
Implement a hash set from scratch supporting add, remove, and contains in O(1) average time.
Pick a random index of a target value with equal probability using reservoir sampling over the array.
Find the most common word not in a banned list by normalizing input and using a frequency counter.
Determine if cards can be rearranged into consecutive groups of size groupSize using a sorted frequency map.
Count subarrays with sum divisible by k using prefix mod frequencies and the complement map pattern.
Count pairs summing to a power of 2 by checking all 22 powers-of-2 against a running frequency map.
Design a time-stamped key-value store supporting set and get(key, timestamp) using binary search on sorted timestamps.
Count 4-tuples summing to zero by splitting into two pairs and using a frequency map of pair sums.
Implement an LFU cache with O(1) get/put using three hash maps: key→val, key→freq, freq→OrderedDict.
Design a data structure supporting inc/dec and getMaxKey/getMinKey in O(1) using a doubly linked list of frequency buckets.
Design Twitter with follow/unfollow and getNewsFeed using per-user tweet lists and a min-heap merge.
Find all index pairs forming palindromes by checking prefix/suffix splits against a reverse-word map.
Complete cheatsheet for Hashing & Maps: all 7 patterns, Big O reference, template code, and MAANG priority guide.
Count the number of connected components of a linked list that consist of nodes in a given set.
Find the longest substring without duplicate characters using a HashSet-backed shrinkable sliding window.
Find the maximum sum of a subarray with all unique elements using a sliding window backed by a HashSet.