Skip to content

Instantly share code, notes, and snippets.

@korniichuk
Created July 24, 2019 17:58
Show Gist options
  • Save korniichuk/aedf635ce23c656c6ebc00471e237b28 to your computer and use it in GitHub Desktop.
Save korniichuk/aedf635ce23c656c6ebc00471e237b28 to your computer and use it in GitHub Desktop.
INNER JOIN for sorted lists
def func(a, b):
result = []
x, y = 0, 0
while (x < len(a)) and (y < len(b)):
if a[x] > b[y]:
y += 1
elif b[y] > a[x]:
x += 1
else:
result.append(a[x])
x += 1
y += 1
return set(result)
a = [1, 2, 3, 4, 5, 7, 8]
b = [3, 4, 5, 6, 7]
func(a, b) # [3, 4, 5, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment