Skip to content

Instantly share code, notes, and snippets.

@froggomad
Created May 6, 2020 15:30
Show Gist options
  • Save froggomad/e672a9564a9ed143b286d96212cb41e4 to your computer and use it in GitHub Desktop.
Save froggomad/e672a9564a9ed143b286d96212cb41e4 to your computer and use it in GitHub Desktop.
//Given two arrays, write a function to compute their intersection.
//Example 1:
//
//Input: nums1 = [1,2,2,1], nums2 = [2,2]
//Output: [2]
//Example 2:
//
//Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
//Output: [9,4]
//Note:
//
//Each element in the result must be unique.
//The result can be in any order.
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
let numSet = Set(nums1)
return Array(numSet.intersection(nums2))
}
intersection([1,2,2,1], [2,2])
intersection([4,9,5], [9,4,9,8,4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment