0

I've been writing functions that find the union and intersection of two sets in Java, but I appear to have a problem in my algorithm somewhere. For example, when I feed in the following two vectors of numbers:

A = {0, 1, 3, 4, 5}
B = {1, 1, 2, 3, 4, 5, 6, 7, 8}

I receive the following:

Union = {1, 1, 2, 3, 4, 5, 6, 7, 8}
Intersection = {0, 1, 3, 4, 5}

Which is clearly incorrect. I should be receiving:

Union = {0, 1, 2, 3, 4, 5, 6, 7, 8}
Intersection = {1, 3, 4, 5}

Here's the code in my main pertaining to the intersection/union:

    Vector<Inty> p1Shingles = new Vector<Inty>();
    p1Shingles.add(new Inty(0));
    p1Shingles.add(new Inty(1));
    p1Shingles.add(new Inty(3));
    p1Shingles.add(new Inty(4));
    p1Shingles.add(new Inty(5));

    Vector<Inty> p2Shingles = new Vector<Inty>();
    p2Shingles.add(new Inty(1));
    p2Shingles.add(new Inty(1));
    p2Shingles.add(new Inty(2));
    p2Shingles.add(new Inty(3));
    p2Shingles.add(new Inty(4));
    p2Shingles.add(new Inty(5));
    p2Shingles.add(new Inty(6));
    p2Shingles.add(new Inty(7));
    p2Shingles.add(new Inty(8));        

    Vector<Inty> shinglesUnion = vectorUnion(p1Shingles, p2Shingles);
    Vector<Inty> shinglesIntersection = vectorIntersection(p1Shingles, p2Shingles);

Here, Inty is a class I created so that I can change the values of the integers I need to store in a vector, which is not possible with the Integer class. Here are the functions I've written:

private static <T> Vector<T> vectorIntersection(Vector<T> p1Shingles, Vector<T> p2Shingles)
{
    Vector<T> intersection = new Vector<T>();

    for(T i : p1Shingles)
    {
        if(p2Shingles.contains(i))
        {
            intersection.add(i);
        }
    }

    return intersection;
}

private static <T> Vector<T> vectorUnion(Vector<T> p1Shingles, Vector<T> p2Shingles) {
    Vector<T> union = new Vector<T>();

    union.addAll(p2Shingles);

    for(T i : p1Shingles)
    {
        if(!p2Shingles.contains(i))
        {
            union.add(i);
        }
    }

    return union;
}

If anyone could give any insight as to why this is not working, I'd love to hear it. Thanks in advance!

user3911542
  • 43
  • 1
  • 5

1 Answers1

1

The method isDuplicated does not use parameter i! Actually I think it always returns True. Replacing the entire code of the function by

return p2Shingles.contains(i)

should be enough.

ipinyol
  • 336
  • 2
  • 12
  • That's an excellent point! That function was wrong and I replaced it. Unfortunately, this leads to my example input sets being processed as follows: – user3911542 Aug 05 '14 at 22:30
  • Union = {1, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 3, 4, 5}, Intersection = {empty} – user3911542 Aug 05 '14 at 22:31
  • Thanks for the help, though! Do you have any other ideas? I really don't see how this doesn't work. – user3911542 Aug 05 '14 at 22:31
  • I've edited the original question replaced the isDuplicated function, per the above suggestion. – user3911542 Aug 05 '14 at 22:54
  • Be careful with the use of **contains**. It returns true if and only if the same object is there. Notice that you are creating different objects when adding them into the vectors (new Inty(0) and so...). I suggest. When you do two times **new Inty(0)** you are creating two different objects even when the content of the object stores a zero in both cases. If you try adding the same object when referring to the same number it will work properly. – ipinyol Aug 05 '14 at 23:14
  • AHA! Brilliant! Thank you! That's what it was! You're absolutely correct! – user3911542 Aug 05 '14 at 23:37