set-intersection

Computing set intersection in linear time?

亡梦爱人 提交于 2019-11-26 09:02:13
问题 Is there an algorithm that, given two sets, computes their intersection in linear time? I can run two for loops to check all pairs of elements, recording elements that I find in both of the sets. However, the runninng time will be O(n 2 ). How do I do this in O(n) time? 回答1: That depends on your set implementation. If you have a hash set (O(1) lookup), then the approach indicated by all the other posters is correct. Iterate across all the elements in the first set. If it's in the second set,

Difference and intersection of two arrays containing objects

倾然丶 夕夏残阳落幕 提交于 2019-11-26 08:16:53
问题 I have two arrays list1 and list2 which have objects with some properties; userId is the Id or unique property: list1 = [ { userId: 1234, userName: \'XYZ\' }, { userId: 1235, userName: \'ABC\' }, { userId: 1236, userName: \'IJKL\' }, { userId: 1237, userName: \'WXYZ\' }, { userId: 1238, userName: \'LMNO\' } ] list2 = [ { userId: 1235, userName: \'ABC\' }, { userId: 1236, userName: \'IJKL\' }, { userId: 1252, userName: \'AAAA\' } ] I\'m looking for an easy way to execute the following three

Python: simple list merging based on intersections

雨燕双飞 提交于 2019-11-26 08:02:47
Consider there are some lists of integers as: #-------------------------------------- 0 [0,1,3] 1 [1,0,3,4,5,10,...] 2 [2,8] 3 [3,1,0,...] ... n [] #-------------------------------------- The question is to merge lists having at least one common element. So the results only for the given part will be as follows: #-------------------------------------- 0 [0,1,3,4,5,10,...] 2 [2,8] #-------------------------------------- What is the most efficient way to do this on large data (elements are just numbers)? Is tree structure something to think about? I do the job now by converting lists to sets and

Python: simple list merging based on intersections

一个人想着一个人 提交于 2019-11-26 03:29:59
问题 Consider there are some lists of integers as: #-------------------------------------- 0 [0,1,3] 1 [1,0,3,4,5,10,...] 2 [2,8] 3 [3,1,0,...] ... n [] #-------------------------------------- The question is to merge lists having at least one common element. So the results only for the given part will be as follows: #-------------------------------------- 0 [0,1,3,4,5,10,...] 2 [2,8] #-------------------------------------- What is the most efficient way to do this on large data (elements are just

Efficient list intersection algorithm

馋奶兔 提交于 2019-11-26 02:31:58
问题 Given two lists (not necessarily sorted), what is the most efficient non-recursive algorithm to find the intersection of those lists? 回答1: You could put all elements of the first list into a hash set. Then, iterate the second one and, for each of its elements, check the hash to see if it exists in the first list. If so, output it as an element of the intersection. 回答2: You might want to take a look at Bloom filters. They are bit vectors that give a probabilistic answer whether an element is a

Best way to find the intersection of multiple sets?

我怕爱的太早我们不能终老 提交于 2019-11-25 20:32:10
I have a list of sets: setlist = [s1,s2,s3...] I want s1 ∩ s2 ∩ s3 ... I can write a function to do it by performing a series of pairwise s1.intersection(s2) , etc. Is there a recommended, better, or built-in way? sth From Python version 2.6 on you can use multiple arguments to set.intersection() , like u = set.intersection(s1, s2, s3) If the sets are in a list, this translates to: u = set.intersection(*setlist) where *a_list is list expansion As of 2.6, set.intersection takes arbitrarily many iterables. >>> s1 = set([1, 2, 3]) >>> s2 = set([2, 3, 4]) >>> s3 = set([2, 4, 6]) >>> s1 & s2 & s3