public
Created

Set Intersection algorithm O(nlogn) time. Code in Python.

  • Download Gist
Intersection.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.