Questions tagged [set-intersection]

The intersection of two or more sets consists of the elements that those sets all have in common.

Set intersection is a mathematical operation defined upon sets.
The intersection of two or more sets is defined as the set of those elements that are present in all sets.

For example, let A = { 2, 3, 4 } and B = { 2, 4, 5 }.
The intersection of A and B is what the set of elements that A and B have in common: { 2, 4 }.
The symbol for intersection is ∩, so in our example we would write A ∩ B = { 2, 4 }

276 questions
14
votes
4 answers

Algorithm / Data structure for largest set intersection in a collection of sets with a given set

I have a large collection of several million sets, C. The elements of my sets come from a universe of about 2000 possible elements. I need to know, for a given set, s, which set in C has the largest intersection with s? (Or the k sets in C with the…
newmanne
  • 2,019
  • 5
  • 22
  • 31
13
votes
4 answers

C++ - Finding intersection of two ranges

What is the best way to find the intersection of two ranges in C++? For example, if I have one range as [1...20] inclusive, and another as [13...45] inclusive, I want to get [13...20], as that is the intersection between them. I thought about using…
1110101001
  • 4,662
  • 7
  • 26
  • 48
11
votes
4 answers

Returning string matches between two lists for a given number of elements in a third list

I've got a feeling that I will be told to go to the 'beginner's guide' or what have you but I have this code here that goes does = ['my','mother','told','me','to','choose','the'] it = ['my','mother','told','me','to','choose','the'] work = [] while…
11
votes
4 answers

Custom intersect in lambda

I would like to know if this is possible to solve using a lambda expression: List listOne = service.GetListOne(); List listTwo = service.GetListTwo(); List result = new List(); foreach(var one in listOne) { foreach(var two…
Johan
  • 35,120
  • 54
  • 178
  • 293
11
votes
2 answers

How to use MongoDB aggregation for general purpose set operations (union, intersection, difference)

I have come across some special purpose implementation of set operations, but nothing for the general case. What is the general case for performing set operations (specifically intersection, union, symmetric difference). This is easier to figure…
Scott
  • 16,711
  • 14
  • 75
  • 120
11
votes
6 answers

intersection of three sets in python?

Currently I am stuck trying to find the intersection of three sets. Now these sets are really lists that I am converting into sets, and then trying to find the intersection of. Here's what I have so far: for list1 in masterlist: list1 =…
thephfactor
  • 181
  • 1
  • 2
  • 8
10
votes
1 answer

What is the complexity of set_intersection in C++?

What is the complexity of the following code? set S1, S2, ans; set_intersection(S1.begin(), S1.end(), S2.begin(), S2.end(), inserter(ans, ans.begin())) where S1 and S2 are some non_empty sets and ans is an empty set. I know that inserting a…
Farzam
  • 1,272
  • 3
  • 14
  • 25
10
votes
3 answers

How to find if all elements of list are in a set in Java 8?

While it is easy to do it in a for loop, is there a way in Java-8 to find if all elements in list L are present in Set s ?
Tanvi Jaywant
  • 375
  • 1
  • 6
  • 18
10
votes
2 answers

Efficient set intersection of a collection of sets in C++

I have a collection of std::set. I want to find the intersection of all the sets in this collection, in the fastest manner. The number of sets in the collection is typically very small (~5-10), and the number of elements in each set is is usually…
Paresh
  • 542
  • 2
  • 6
  • 15
9
votes
3 answers

What happens if I use vector::begin() instead of std::back_inserter(vector) for output of set_intersection?

I have been using the highly concise and intuitive C++ syntax for finding the intersection of two sorted vectors and putting the result in a third vector: vector a,b,c; //... std::set_intersection(a.begin(),a.end(),b.begin(),b.end(), …
kdog
  • 1,583
  • 16
  • 28
9
votes
1 answer

What's the algorithm of 'set.intersection()' in python?

First of all, my purpose is to randomly get only one element in both known sets. So my original method is firstly intersect two sets. And then randomly pick up a element from the intersected set. But this is foolish, because that I only need a…
Ding Ding
  • 295
  • 3
  • 7
8
votes
2 answers

Practical Use of Reversed Set Operators in Python

Is there any difference between using the reversed union or intersection operators on sets in Python? for instance, s & z corresponds to s.__and__(z) z & s corresponds to s.__rand__(z) If these are supposed to return the same thing, why have the…
MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
8
votes
4 answers

Indices that intersect and sort two numpy arrays

I have two numpy arrays of integers, both of length several hundred million. Within each array values are unique, and each is initially unsorted. I would like the indices to each that yield their sorted intersection. For example: x = np.array([4,…
JoeZuntz
  • 1,092
  • 10
  • 16
7
votes
3 answers

STL set intersection and the output

I have a code snippet like this, to be compiled under VC++ 2010. std::set s1; std::set s2; std::set res_set; std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), res_set.begin()); As far…
progician
  • 910
  • 8
  • 15
7
votes
4 answers

set_intersection for two different types of sets

Is there any way to do std::set_intersection on two different types of sets? I have two sets: std::set l_set1; std::set l_set2; I'm able to define some comparator for them that checks if X1 and X2 are equal. struct sample_comparer { …
matekm
  • 5,990
  • 3
  • 26
  • 31
1
2
3
18 19