HomeArrays & Hashing
Arrays & Hashing
Trade memory for speed — hash maps turn lookups into O(1).
51 shown
- Contains DuplicateA set remembers everything you've seenanimatedEasy
- Two SumUnsorted input · remember complements in a mapanimatedEasy
- Valid AnagramSame letters, same countsanimatedEasy
- Group AnagramsBucket words by their sorted signatureanimatedMedium
- Top K Frequent ElementsBucket by frequency, read from the topanimatedMedium
- Product of Array Except SelfPrefix × suffix, no divisionanimatedMedium
- Longest Consecutive SequenceOnly start counting from sequence headsanimatedMedium
- Practice problems
- Basic HashingA tally you can query instantlyEasy
- Counting Frequencies of Array ElementsOne pass to tally, one to reportanimatedEasy
- Highest Occurring Element in an ArrayTally, then take the maxanimatedEasy
- Largest ElementOne pass, keep the best so faranimatedEasy
- Second Largest ElementTrack the best two, in one passanimatedEasy
- Check if the Array is Sorted IICompare each neighbour pairanimatedEasy
- Remove duplicates from Sorted arrayTwo pointers: write index and read indexEasy
- Left Rotate Array by OneSave the first, shift left, put it backEasy
- Left Rotate Array by K PlacesReverse three timesEasy
- Move Zeros to EndWrite the non-zeros forward, then padanimatedEasy
- Linear SearchScan until you find itanimatedEasy
- Union of two sorted arraysMerge two sorted lists, skipping repeatsEasy
- Find missing numberCompare the expected sum with the real oneanimatedEasy
- Maximum Consecutive OnesCount the run, reset on a zeroanimatedEasy
- Find the number that appears once, and other numbers twice.XOR everything; pairs cancelanimatedMedium
- Longest subarray with given sum K(positives)Sliding window, because all values are positiveanimatedMedium
- Longest subarray with sum KPrefix sums plus a map of first occurrencesanimatedMedium
- Sort an array of 0's 1's and 2'sDutch national flag: three pointers, one passanimatedMedium
- Majority Element-IBoyer-Moore vote cancellinganimatedEasy
- Kadane's AlgorithmDrop the prefix the moment it turns negativeanimatedMedium
- Print subarray with maximum subarray sum (extended version of above problem)Kadane, remembering where the run begananimatedMedium
- Stock Buy and SellTrack the cheapest day so faranimatedMedium
- Rearrange array elements by signTwo write pointers, even and odd slotsMedium
- Next PermutationFind the pivot, swap the successor, reverse the tailMedium
- Leaders in an ArrayScan from the right, keep the running maxanimatedMedium
- Longest Consecutive Sequence in an ArrayOnly start counting at a run's first elementanimatedMedium
- Set Matrix ZeroesUse the first row and column as the marker boardanimatedMedium
- Rotate matrix by 90 degreesTranspose, then reverse each rowanimatedMedium
- Print the matrix in spiral mannerFour moving boundaries closing inwardsanimatedMedium
- Count subarrays with given sumCount prefix sums you have already seenanimatedMedium
- Pascal's Triangle IOne binomial coefficient, computed iterativelyEasy
- Majority Element-IIBoyer-Moore with two candidatesHard
- 3 SumSort, fix one, two-point the restanimatedMedium
- 4 SumTwo fixed elements, then two pointersMedium
- Largest Subarray with Sum 0Two equal prefix sums bracket a zero-sum runanimatedMedium
- Count subarrays with given xor KThe same prefix trick, with XOR instead of sumanimatedHard
- Merge Overlapping SubintervalsSort by start, extend or pushanimatedMedium
- Merge two sorted arrays without extra spaceFill from the back, largest firstMedium
- Find the repeating and missing numberTwo equations, two unknownsHard
- Count InversionsCount across the merge stepanimatedHard
- Reverse PairsA separate counting pass inside merge sortanimatedHard
- Maximum Product Subarray in an ArrayTrack the smallest product tooanimatedHard
- OverviewCount, dedupe and look up in O(1)animatedMedium
- Encode and Decode StringsLength-prefix each stringMedium