0

I want to delete duplicates totally from hashsets.

For example:

  • hashset names1 contains a, b, c, d
  • hashset names2 contains x, y, z, a

I want that hashset names3 should have b, c, d, x, y, z

Deleting the common element between the 2 hashsets and storing it in third hashset.how to do this?

Mzf
  • 5,210
  • 2
  • 24
  • 37

5 Answers5

1

You want the Union of the two HashSets, minus the intersection. So, basically, the unique items from both sets:

public void union(Set<E> s){
    set.addAll(s);
}

public void intersection(Set<E> s){ 
    set.retainAll(s);
}

public void unique(Set<E> s){
   set.addAll( set.union(s).removeAll( set.intersection(s) );
}
jedison
  • 908
  • 6
  • 15
  • The idea is sound, but the implementation seems very odd. First, all those operations modify `set`, so I guess there would be all sorts of unwanted behaviour. Also, `union` and `intersection` don't return anything, so this won't even compile. If this is pseudo-code, please mark it as such. – tobias_k Apr 24 '14 at 15:31
  • I'd love to provide working code, but then we need to see the code of @user3569467. If this is someone's homework, then I'm happy to advice but not do it for them. – jedison Apr 24 '14 at 15:32
1

To find the unique elements, you need to find the union of both sets, minus their intersection.

You can achieve this using the addAll, retainAll and removeAll methods of Set:

Set union = new HashSet(setA);
union.addAll(setB);

Set intersection = new HashSet(setA);
intersection.retainAll(setB);

Set unique = new HashSet(union);
unique.removeAll(intersection);
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • 1
    This is the correct (and functioning) solution. Here's an [ideone](http://ideone.com/mbV1WD) for you, OP. – 2rs2ts Apr 24 '14 at 15:45
0

you can find the intersection between 2 list and than remove than from the union list:

find all intersection

Set<String> uniques = new HashSet<String>(names1);
uniqueNums.retainAll(names2);
Mzf
  • 5,210
  • 2
  • 24
  • 37
0

As others have said, this is the union minus the intersection. To implement that easily, you can use Apache Commons Collections or Google Guava libraries. Here's the code using the Commons Collections CollectionUtils class:

Collection result = CollectionUtils.union(set1, set2);
result.removeAll(CollectionUtils.intersection(set1, set2));

result will be a Collection, which can be converted to a set using new HashSet(result)


Here's the implementation using Google's Guava library Sets class:

HashSet result = new HashSet(Sets.union(set1, set2));
result.removeAll(Sets.intersection(set1, set2));
E-Riz
  • 31,431
  • 9
  • 97
  • 134
0

If you dont want to modify the Set1 and Set2.

1. Find Largest Set and add to new one.
2. Iterate Small Set and if it already exists remove , else add it.
public static <E> Set<E>  getIntersection(Set<E> set1, Set<E> set2) {
        boolean set1IsLarger = set1.size() > set2.size();
        Set<E> smallerSet = set1IsLarger ? set2 : set1;
        Set<E> largerSet = set1IsLarger ? set1 : set2;
        Set<E> intersection = new HashSet<E>(set1.size()+set2.size());
        intersection.addAll(largerSet);
        for (E value : smallerSet) {
            if (!largerSet.contains(value)){
                intersection.add(value);
            }else{
                intersection.remove(value);
            }
        }
        return intersection;
    }
Mani
  • 3,274
  • 2
  • 17
  • 27