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?
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:
Ideone: http://ideone.com/reGDBy
Example usage:
Example of evaluation:
oncecontains {2,4,6,8,9} andtwiceis empty.onceset, so they are added to thetwiceset.onceis now {2,4,6,8,9},twiceis now {2,8,9}.twice, 4 is added totwice, 8, 9 are re-added totwice.onceis now {2,4,6,8,9} (union of all sets),twiceis now {2,4,8,9} (elements that occur at least twice).twicefromonce.onceis now {6}. Returnonce.