-4
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;
} 
sanbhat
  • 17,522
  • 6
  • 48
  • 64
mera
  • 1
  • 3
    Topic 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 Answers1

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