DEV Community

Discussion on: A common coding interview question

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

But [...s1].filter(x => s2.has(x)) is still O(n²), isn't it?

  • filter() will step through s1 (=outer forEach)
  • s2.has() will check against each element in s2 (=inner forEach), probably exiting early

I'm pretty sure there is a O(s1) + O(s2) = O(n) solution.

Collapse
 
sleepingnapster profile image
sleepingnapster

Good answer but you lost the ordering when using sets.