Intersection of Two Arrays II
Recognize the pattern
Brute force idea
If you approach Intersection of Two Arrays II in the most literal way possible, you get this: For each element in array1, search array2. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.
Better approach
The real unlock in Intersection of Two Arrays II comes when you notice this: Use a hash map to count frequencies in one array, then check against the other. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
Key invariant
The compass for Intersection of Two Arrays II is this: The count map tracks remaining available matches; decrement on each match. As long as that statement keeps holding, you can trust the steps built on top of it.
Watch out for
The trap in Intersection of Two Arrays II usually looks like this: Not handling duplicate matches — decrement the count after each match to avoid reusing. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.