Answer would be Option 2 -- O(n^2)
because
// loops n times i.e. O(n)
return second.reduce((acc, current) => {
// spread operator operation would be O(n) every time
return firstSet.has(current) ? [...acc, current] : acc;
}, []);
Hence, overall time complexity would be O(n^2)
.
P.S. This is not the best way to find intersection and approach can be improved so please do not use this code anywhere. Code is written in a certain way to test logic.
For useful and amazing frontend and programming tutorials: https://bit.ly/devtools-yt