ravi
ravi

Reputation: 6328

Find all the elements which has no duplicate in multiple HashSet of Integers in Java

In multiple HashSet of Integers I want to get all those elements, which has no duplicate. i.e. which came only once in union of all the HashSet. I am not able to conceptualize it programmatically.

As an example, consider set first contains {2,4,6,8,9}, second set contains {2,8,9} and third set contains {2,4,8,9}. In all these set, element 6 occurs only once.

How to find all the elements which has no duplicate in multiple HashSet of Integers in Java?

Upvotes: 5

Views: 1596

Answers (7)

Bibhu prasad Mishra
Bibhu prasad Mishra

Reputation: 1

public class test {

public static void main(String[] args) throws Exception, IOException {

    int count=0;
    HashSet<Integer> set1 = new HashSet<Integer>();
    HashMap<Integer, String> ee=new HashMap<Integer,String>();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("how many elements u want to store");
    int n=Integer.parseInt(br.readLine());
    System.out.println("enter te element u want insert");
    for(int i=0;i<n;i++)
    {
        boolean x=set1.add(Integer.parseInt(br.readLine()));
        if(x==false)
        {
            count++;
        }
    }
    System.out.println("no of duplicate elements is   "+count);
    }
}

Upvotes: -1

John Dvorak
John Dvorak

Reputation: 27287

You could hold the set of elements that occur at least once and at least twice. It's a bit of manual looping but it's possible. This will work for any number of sets to difference and will not modify the input:

public static Set<E> unique(Set<? extends E>... sets){
   Set<E> once = new HashSet<E>();
   Set<E> twice = new HashSet<E>();

   for(Set<? extends E> set:sets){
      for(E el:set){
         if(once.contains(el)){
            twice.add(el);
         } else {
            once.add(el);
         }
      }
   }

   once.removeAll(twice);
   return once;
} 

Ideone: http://ideone.com/reGDBy

Example usage:

Set<Integer> set1, set2, set3;
...
Set<Integer> u = unique(set1, set2, set3);

Example of evaluation:

As an example, consider set first contains {2,4,6,8,9}, second set contains {2,8,9} and third set contains {2,4,8,9}. In all these set, element 6 occurs only once.

  • After the first inner loop completes, once contains {2,4,6,8,9} and twice is empty.
  • Adding the second set: 2, 8 and 9 are already in the once set, so they are added to the twice set.
  • once is now {2,4,6,8,9}, twice is now {2,8,9}.
  • From the third set: 2 is re-added to twice, 4 is added to twice, 8, 9 are re-added to twice.
  • once is now {2,4,6,8,9} (union of all sets), twice is now {2,4,8,9} (elements that occur at least twice).
  • remove twice from once. once is now {6}. Return once.

Upvotes: 4

Natix
Natix

Reputation: 14257

A Guava version using intermediate Multiset:

@SafeVarargs
public static <E> Set<E> uniqueElements(Set<? extends E>... sets) {
    final Multiset<E> multiset = HashMultiset.create();
    for (Set<? extends E> set : sets) {
        multiset.addAll(set);
    }
    return Sets.filter(multiset.elementSet(), new Predicate<E>() {
        @Override
        public boolean apply(E element) {
            return multiset.count(element) == 1;
        }
    });
}

Upvotes: 1

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

You could do that using the contains() method. First, create a new HashSet from all the other sets. Then iterate over this set and check if other sets contains() the specified element. If two or more lists contain it, then you have a duplicate and can continue. If only one set contains the element, you can store it somewhere in a different result set for example.

I wrote a utility method to achieve what you need:

public static <E> HashSet<E> uniques(HashSet<E>... sets){
    HashSet<E> everything = new HashSet<E>();
    for(HashSet<E> set : sets){
        everything.addAll(set);
    }
    HashSet<E> uniques = new HashSet<E>();
    for(E e : everything){
        int count = 0;
        for(HashSet<E> set : sets){
            if(set.contains(e)){
                count++;
            }
            if(count > 1){
                break;
            }
        }
        if(count == 1){
            uniques.add(e);
        }

    }
    return uniques;
}

Upvotes: 1

djechlin
djechlin

Reputation: 60778

Create a multiset and iterate through it pulling out all elements with count 1. O(n).

Upvotes: 0

MTilsted
MTilsted

Reputation: 5545

How about creating 2 new Hashsets. Called seenOnce and seenMoreThenOnce.

Then you iterate over all your integers in the different hashmaps.

For each integer:
   If it is in seenMoreThenOnce do nothing.
   else If it is in seenOnce, remove it from seenOnce and add it to seenMoreThenOnce
   Else add it to seenOnce.

When you are done iterating over all your hashmaps, seenOnce will contain the integers seen only once.

Upvotes: 0

Achintya Jha
Achintya Jha

Reputation: 12843

 public static void main(String[] args) {
        HashSet<Integer> set1 = new HashSet<Integer>();

        set1.add(2);
        set1.add(4);
        set1.add(6);
        set1.add(8);
        set1.add(9);
        HashSet<Integer> set2 = new HashSet<Integer>();
        set2.add(2);
        set2.add(8);
        set2.add(9);
        HashSet<Integer> set3 = new HashSet<Integer>();
        set3.add(2);
        set3.add(4);
        set3.add(8);
        set3.add(9);
        set1.removeAll(set2);
        set1.removeAll(set3);
        System.out.println(set1);
    }

Upvotes: 0

Related Questions