# Duplicates in more than two sets

• 12-03-2009, 02:59 PM
JavaJ
Duplicates in more than two sets
Good day...

I have a map which is declared to hold String and TreeSet integer.
like so :
Name : 10 , 20 , 40
Name2 : 12 , 33, 50
Name3: 14, 50, 20
Name4: 20,10,80
..etc

I want to ensure that each name has unique set of integers no duplicates allowed even if the duplicate number is in another name's set..

for two sets I can compare directly and remove duplicates but what is the best way to ensure that there is no duplicates while there are more than two sets ??

• 12-03-2009, 03:22 PM
JosAH
Have a look at the Set.removeAll( ... ) method; it returns true if the Set has changed because of the method.

kind regards,

Jos
• 12-03-2009, 03:39 PM
JavaJ
JosAh

If I got it right... the problem still exists I should repeat this multiple times..
someVar.get("Name").removeAll(someVar.get("Name1") );
someVar.get("Name").removeAll(someVar.get("Name2") );

...etc

regards
• 12-03-2009, 03:41 PM
JosAH
Quote:

Originally Posted by JavaJ
JosAh

If I got it right... the problem still exists I should repeat this multiple times..
someVar.get("Name").removeAll(someVar.get("Name1") );
someVar.get("Name").removeAll(someVar.get("Name2") );

...etc

regards

Yep, you have to take the 'difference' of all the sets, if any of those two sets have a non-empty intersection the first set is changed and so the removeAll method returns true.

kind regards,

Jos
• 12-03-2009, 03:48 PM
JavaJ
Thanks for your quick response .. one more question :
suppose I have two tables of these
table 1
Name : 10 , 20 , 40
Name2 : 12 , 33, 50
Name3: 14, 50, 20
Name4: 20,10,80

table2
... etc

How can I ensure that table 2 also doesn't have any duplicates from the first table integer sets ..?

Best Regards
• 12-03-2009, 04:07 PM
JosAH
Take the union of all sets in the first table and use that to removeAll( ... ) for all the sets in the second table. If something is actually removed the intersection of the two tables wasn't empty. If your data survives this test do the first test per table.

kind regards,

Jos
• 12-03-2009, 04:32 PM
Tolls
How are these things being populated?
Wouldn't it be better to have some sort of master set. As you add numbers into the other sets, you add them into the master set first and only add them to these other sets if they didn't already exist in the master.

Something along those lines anyway.
• 12-03-2009, 04:57 PM
JavaJ
They are populated thru a populatemethod(name[], name2[], name3[],name4[])
• 12-03-2009, 05:07 PM
Tolls
So you have a method that takes several arrays of numbers?

Then I'd use my method to ensure uniqueness, as you populate whataver it is you're populating.

That's not to say someone else won't have a better technique...or at least a more stylish one.