Maintained by Vikas Dulgunde, software engineer
You receive two arrays of integers, nums1 and nums2. Your job is to find every value that is present in both arrays and report it.
The answer is a set of values, not a multiset. Even if a shared value appears many times across either input, it belongs in the result exactly once.
Any ordering of the result is accepted. A checker that ignores order will treat your answer as correct as long as the collection of distinct shared values matches.
An empty result is valid: when the two arrays share nothing, you return an empty array.
Only the value 2 lives in both arrays. The duplicate 2s collapse to a single entry in the answer.
9 and 4 appear in both arrays; 5, 8 do not. Order is free, so [4,9] would be equally valid.
The two arrays have no value in common, so the intersection is empty.
Visible test cases
Membership testing is the core operation. What data structure answers 'is x in here?' in constant time?
Build a set from the first array. Then walk the second array and keep the values that the set contains.
To avoid emitting the same value twice, either dedupe the second array first or track which shared values you have already added.
For every value in the first array, scan the entire second array to check whether it appears, then guard against adding a value more than once.
Put the first array into a set so membership checks are constant time, then iterate the second array once, collecting each shared value the first time you see it.