AlgoViz
HomeArrays & Hashing

Arrays & Hashing

Trade memory for speed — hash maps turn lookups into O(1).

0/ 51 understood · 0%
Walkthroughs
38
Problems
51
Start here
51 shown
  1. Contains DuplicateA set remembers everything you've seenEasy
  2. Two SumUnsorted input · remember complements in a mapEasy
  3. Valid AnagramSame letters, same countsEasy
  4. Group AnagramsBucket words by their sorted signatureMedium
  5. Top K Frequent ElementsBucket by frequency, read from the topMedium
  6. Product of Array Except SelfPrefix × suffix, no divisionMedium
  7. Longest Consecutive SequenceOnly start counting from sequence headsMedium
  8. Practice problems
  9. Basic HashingA tally you can query instantlyEasy
  10. Counting Frequencies of Array ElementsOne pass to tally, one to reportEasy
  11. Highest Occurring Element in an ArrayTally, then take the maxEasy
  12. Largest ElementOne pass, keep the best so farEasy
  13. Second Largest ElementTrack the best two, in one passEasy
  14. Check if the Array is Sorted IICompare each neighbour pairEasy
  15. Remove duplicates from Sorted arrayTwo pointers: write index and read indexEasy
  16. Left Rotate Array by OneSave the first, shift left, put it backEasy
  17. Left Rotate Array by K PlacesReverse three timesEasy
  18. Move Zeros to EndWrite the non-zeros forward, then padEasy
  19. Linear SearchScan until you find itEasy
  20. Union of two sorted arraysMerge two sorted lists, skipping repeatsEasy
  21. Find missing numberCompare the expected sum with the real oneEasy
  22. Maximum Consecutive OnesCount the run, reset on a zeroEasy
  23. Find the number that appears once, and other numbers twice.XOR everything; pairs cancelMedium
  24. Longest subarray with given sum K(positives)Sliding window, because all values are positiveMedium
  25. Longest subarray with sum KPrefix sums plus a map of first occurrencesMedium
  26. Sort an array of 0's 1's and 2'sDutch national flag: three pointers, one passMedium
  27. Majority Element-IBoyer-Moore vote cancellingEasy
  28. Kadane's AlgorithmDrop the prefix the moment it turns negativeMedium
  29. Print subarray with maximum subarray sum (extended version of above problem)Kadane, remembering where the run beganMedium
  30. Stock Buy and SellTrack the cheapest day so farMedium
  31. Rearrange array elements by signTwo write pointers, even and odd slotsMedium
  32. Next PermutationFind the pivot, swap the successor, reverse the tailMedium
  33. Leaders in an ArrayScan from the right, keep the running maxMedium
  34. Longest Consecutive Sequence in an ArrayOnly start counting at a run's first elementMedium
  35. Set Matrix ZeroesUse the first row and column as the marker boardMedium
  36. Rotate matrix by 90 degreesTranspose, then reverse each rowMedium
  37. Print the matrix in spiral mannerFour moving boundaries closing inwardsMedium
  38. Count subarrays with given sumCount prefix sums you have already seenMedium
  39. Pascal's Triangle IOne binomial coefficient, computed iterativelyEasy
  40. Majority Element-IIBoyer-Moore with two candidatesHard
  41. 3 SumSort, fix one, two-point the restMedium
  42. 4 SumTwo fixed elements, then two pointersMedium
  43. Largest Subarray with Sum 0Two equal prefix sums bracket a zero-sum runMedium
  44. Count subarrays with given xor KThe same prefix trick, with XOR instead of sumHard
  45. Merge Overlapping SubintervalsSort by start, extend or pushMedium
  46. Merge two sorted arrays without extra spaceFill from the back, largest firstMedium
  47. Find the repeating and missing numberTwo equations, two unknownsHard
  48. Count InversionsCount across the merge stepHard
  49. Reverse PairsA separate counting pass inside merge sortHard
  50. Maximum Product Subarray in an ArrayTrack the smallest product tooHard
  51. OverviewCount, dedupe and look up in O(1)Medium
  52. Encode and Decode StringsLength-prefix each stringMedium