Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Set Intersection algorithm O(nlogn) time. Code in Python.
from itertools import chain
intersection = []
sets = [[0,4,5,2,1],[1,3,6,2,4],[4,1,2,5,7,0]]
merged = list(chain.from_iterable(sets))
i = 0
while i < (len(merged)-(len(sets)-1)):
sub = merged[i:(i+len(sets))]
if sub[0] == sub[len(sub)-1]:
i += (len(sub)-1)
i += 1
print intersection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.