Skip to content

Conversation

konaraddi
Copy link

https://leetcode.com/problems/intersection-of-two-arrays/

class Solution:
  def intersection(self, arrA: List[int], arrB: List[int]) -> List[int]:

    arrA.sort()
    arrB.sort()

    intersection = []

    aIndex = 0
    bIndex = 0
    recentOverlap = None

    while aIndex < len(arrA) and bIndex < len(arrB):
      if arrA[aIndex] > arrB[bIndex]:
        bIndex += 1
      elif arrA[aIndex] == arrB[bIndex]:
        if arrB[bIndex] != recentOverlap: # skip duplicate overlapping elements
          recentOverlap = arrB[bIndex]
          intersection.append(recentOverlap)
        bIndex += 1 # you could increment aIndex instead
      else:
        # arrA[aIndex] < arrB[bIndex]
        aIndex += 1

    return intersection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant