Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution to Leetcode #349 - intersection of two arrays #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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