Mark As Completed Discussion

One Pager Cheat Sheet

  • We need to write a function called intersection which takes two arrays as inputs and returns their intersection as a unique set of values.
  • We can brute force the intersection of two arrays by looping through the first array, checking if each element exists in the second array, and then adding the element to an object to ensure uniqueness of the items in the result array.
  • We can make use of Sets and the filter method to find the intersection of two arrays in O(1) time complexity.
  • The creation of a set from an array of n elements and checking for its existence in a second array of m elements takes O(m+n) time complexity.
  • The problem helps you learn when to use the Set data structure, thereby emphasizing mathematics and interview preparation.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

You're doing a wonderful job. Keep going!

If you had any problems with this tutorial, check out the main forum thread here.