Swift Two Pointers solution with illustration


  • 1
    S
    func intersect(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
        var nums1 = nums1.sorted()
        var nums2 = nums2.sorted()
        var res = [Int]()
        var i = 0, j = 0
        
        while i < nums1.count && j < nums2.count {
            if nums1[i] == nums2[j] {
                res.append(nums1[i])
                i += 1 ; j += 1
            } else if nums1[i] < nums2[j] {
                i += 1
            } else {
                j += 1
            }
        }
        return res
    }
    


Log in to reply
 

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