Swift solution using Dictionary/hashtable, 29ms


  • 1
    S
    class Solution {
        func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
            var hash = [Int: Int]()
            var results = [Int]()
            
            for val in nums1 {
                hash[val] = 1
            }
            for val in nums2 {
                if (hash[val] ?? 0) > 0 {
                    hash[val] = 0
                    results.append(val)
                }
            }
            
            return results
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.