Union and Intersection methods (SetADT)

Hey, so i have this SetADT, and i need to add two methods, Union and Intersection.

- The Union method will return a union of the two sets as an array to the calling program (no duplicates allowed).
- The Intersection method will return the intersection of the two sets as an array to the calling program (no duplicates allowed).

Can anyone guide me in the right direction to adding these two methods?

**SetADT.java**

Code :

package DSA2;
public class SetADT
{
//define fields
private int pointer;
private int[] setArray;
//define methods
public SetADT()
{
pointer = 0;
setArray = new int[5];
}
public void add(int value)
{
if (!contains(value))
{ //check available space
//if not - increase the array size
if (pointer == setArray.length)
{
setArray = increaseSize();
}
setArray[pointer] = value;
pointer++;
}
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
for(int index=0; index<pointer; index++)
{
sb.append(setArray[index]);
sb.append(", ");
}
return sb.toString();
}//toString
public boolean contains(int value)
{
for(int index=0; index < setArray.length; index++)
{
if(setArray[index]== value)
{
return true;
}
}
return false;
}//contains
private int[] increaseSize()
{
int size = setArray.length;
int [] newArr = new int[size + 5];
System.arraycopy(setArray, 0, newArr, 0, setArray.length);
return newArr;
}
public int[] toArray()
{
int [] tmpArray = new int[pointer];
System.arraycopy(setArray, 0, tmpArray,0, pointer);
return tmpArray;
}//toArray
}

Thanks in advance!

Jason.

Re: Union and Intersection methods (SetADT)

What are your ideas for how to implement each of these methods?

Do you have two arrays of int values to scan through and make some decisions about using their contents to make a third array.

Are the arrays sorted?

Re: Union and Intersection methods (SetADT)

Code :

package DSA2;
public class TestSet
{
public static void main(String[] args)
{
SetADT set1 = new SetADT();
SetADT set2 = new SetADT();
set1.add(1);
set1.add(2);
set1.add(4);
set1.add(5);
set1.add(7);
set2.add(1);
set2.add(2);
set2.add(3);
set2.add(6);
set2.add(7);
set2.add(8);
System.out.println(set1.toString());
System.out.println();
System.out.println(set2.toString());
int [] setItems1 = set1.toArray();
int [] setItems2 = set2.toArray();
System.out.println(setItems1.length);
System.out.println(setItems2.length);
}
}

This is my test code, which adds several numbers to two different sets.

I need to figure out how to implement the two methods, one producing the union, and the other the intersection of the two sets. I'm not sure how to do this, any help would be much appreciated.

Thanks, Jason.

Re: Union and Intersection methods (SetADT)

What are your ideas for how to implement each of these two methods?

Do you have two arrays of int values to scan through and make some decisions about using their contents to make a third array.

Are the arrays sorted? If not, having them sorted would make it easier.

Re: Union and Intersection methods (SetADT)

Re: Union and Intersection methods (SetADT)

**What are your ideas for how to implement each of these two methods?**

Maybe combine both arrays and then iterate through to find similar values and removes them ?

**Do you have two arrays of int values to scan through and make some decisions about using their contents to make a third array.**

The arrays both contain int values

**Are the arrays sorted? If not, having them sorted would make it easier. **

No they aren't sorted, I might consider doing this, if needs be

Re: Union and Intersection methods (SetADT)

Sorting the arrays will make the logic much easier.

Given the two arrays: {1,3,4} and {1,2,3}

How do you get their intersection?

There needs to be a separate index for each array. Look at the current element in each array at the location pointed to by the index for that array.