Skip to content

Instantly share code, notes, and snippets.

@aymanfarhat
Created January 4, 2013 19:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aymanfarhat/4455373 to your computer and use it in GitHub Desktop.
Save aymanfarhat/4455373 to your computer and use it in GitHub Desktop.
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))
merged.sort()
i = 0
while i < (len(merged)-(len(sets)-1)):
sub = merged[i:(i+len(sets))]
if sub[0] == sub[len(sub)-1]:
intersection.append(sub[0])
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