public Set intersection(Set set){
Set intersect = new Set(this.count + set.count);
for(int i=1; i<count; i++)
{
if(this.items[i] && set.items[i])
intersect.add(i);
return intersect;
}
Asked
Active
Viewed 202 times
-4
-
3Topic of this question says 'array' but the code says 'set'. What is this question about? – bartektartanus Apr 23 '14 at 07:05
-
possible duplicate of [Best way to find an intersection between two arrays?](http://stackoverflow.com/questions/13270491/best-way-to-find-an-intersection-between-two-arrays) – ioreskovic Apr 23 '14 at 07:07
1 Answers
2
To find the intersection of two sets you can use the function retainAll.
Set<Integer> s1;
Set<Integer> s2;
s1.retainAll(s2);
After this, s1
will contain the intersection.
You could also use this method for arrays if you don't mind converting them to a set or list first, e.g.:
int a[] = {1,2,3};
Set<Integer> mySet = new HashSet<Integer>(Arrays.asList(a));

keyser
- 18,829
- 16
- 59
- 101