aspose file tools*
The moose likes Beginning Java and the fly likes finding intersection of two arrays, remove duplicates w/out error message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finding intersection of two arrays, remove duplicates w/out error message" Watch "finding intersection of two arrays, remove duplicates w/out error message" New topic
Author

finding intersection of two arrays, remove duplicates w/out error message

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I'm creating a method to receive an unordered set as a parameter and returns a new unordered set whose elements are the intersection of this set and the parameter set. So I decided the best way was to go through both arrays seeing if any number of two arrays was the same and storing those numbers into another array called intersectionArray. Is this the best way to go about the solution? It seems like there is a simpler easier way to do this.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Are you using a Set or an array and do you want to hold them or remove them? Because you're talking about all those options. The Set has a methods which retain or remove all the elements that are in the parameter Collection.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I guess I would like to use sets. I think I want to hold them, I'm supposed to take the intersection of sets, kinda like in discrete math "seta ∩ setb", judging by your response i've gone the wrong direction with the code I guess. So I wont be using arrays at all?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
If you look through the Set interface, you will find methods which mirror intersection, union and assymmetrical difference.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

If you don't mind using 3rd party libraries then you could use Commons Collections. It has a union method which takes in 2 collections and returns the union of them as a collection.

Sean


I love this place!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

But that is not what the topic starter wants. And Java has that functionality as well but with one extra step. Just look at Collection.addAll().
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I guess this stuff is pretty new to me and I am having a terrible time figuring out what I need to do. I looked at the set interface link and the only part that looked familiar was the <t> that accepts all parameters and the equals and a few others but I'm still not really understanding how this is supposed to be done I guess. I know i'm supposed to have a method for intersection and another method for union but I'm unfamiliar with "receives an UnorderedSet as a parameter" "UnorderedSet is the name of the Java file I created. "intersection of this set and the parameter set." I guess I'm not really understanding parameter set because I thought a parameter was a type not a set? I guess I would just like a tip or a hint on how this stuff works. It makes sense if I do it in an array but because of the exercise it must be done according to my prompt, "The method intersection( ) receives an UnorderedSet as a parameter and returns a new
UnorderedSet whose elements are the intersection of this set and the parameter set." Can someone maybe explain that in plain English?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You should not create a java class named UnorderedSet. An unordered set is just a set that doesn't have any order (e.g. a HashSet).

Intersection means that it will only take the elements that are in both the sets.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Because of the teachers requirements I have to have a class named UnorderedSet "Your UnorderedSet class should contain appropriate constructors and should override the parent methods insertAt( ) and insertEnd( ) to ensure duplicates are not added. Duplicates should be ignored (no error printed, no exception thrown). Do not replicate code in the parent methods." I finished this part already. I will do a little more research on hashsets because when i'm looking online for help i keep running into them. I understand intersection and union but I'm trying to figure out how to make them work with sets, this is what I have so far. Some of the methods I still haven't gotten around to yet.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
I found out a little more I need to create a new UnorderedSet<T> that will hold the set of elements that are the intersection of this set and
the paramSet and then return that set. You can access the inherited instance variables, length, list, and maxSize each UnorderedSet using objectName. So to access an element of
this set it would be this.list[i] or just list[i], and for the parameter set it would be paramSet.list[i], or paramSet.length or this.length etc.

Also, you want to use the seqSearch method to tell you if an element is in one of the sets. Don't do your own search.

So I got rid of the array stuff and I'm working on creating a new unorderedset from unorderedSet intersecting paramset.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding intersection of two arrays, remove duplicates w/out error message