• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Intersection  RSS feed

 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, as part of a program I must write I have a method void takeIntersectionWith(Object other)which takes an Array object and compares it to another and finds the intersection point.For example, if one bag contains {1,1,3,3,4} and the other bag is {1,1,1,3,7} the intersection is {1,1,3}. The method can't change the other bag. I'm not really sure where to start on this,
Basically all I know is that I'll need to cycle through both arrays and compare the values, but I'm kinda unclear on how to do this. Any advice would be greatly appreciated.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can sort them in ascending order you can use one of my oldest favorites:

"handle match" is where you'll deal with intersections. It would be easiest to create a new third bag (manned by I Don't Know) and put the intersection in there than to munge one of the originals by removing non-intersection items.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: Norville, one of the sherrifs will doubtless ask you (and the rest of the gang to change your user name less obviously fictional.


[ September 22, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!