Find the values two arrays share
Permission checks, tag filters and "also bought" panels all reduce to the same question: which values appear in both lists?
Implement intersection(a, b). Return a new array holding every value from a that also appears in b, in the order they first appear in a, with duplicates removed so each shared value appears exactly once. Compare values the way a Set does: NaN matches NaN, but 1 does not match "1". If there is no overlap, or either array is empty, return an empty array. Never modify either input.
What it has to do
- Include only values present in both arrays.
- Order the result by first appearance in
a. - Include each shared value only once, even if
arepeats it. - Match
NaNwithNaN, and keep1and"1"distinct. - Do not mutate either input array.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.