Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:49:20+00:00 2026-06-15T14:49:20+00:00

In multiple HashSet of Integers I want to get all those elements, which has

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:49:21+00:00Added an answer on June 15, 2026 at 2:49 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to remove multiple elements from a set while I
I have a HashSet that contains multiple lists of integers - i.e. HashSet<List<int>> In
I really just need some way to find all the strings in the hashSet
If multiple event handlers are attached to same event on the same elements, in
If multiple threads call System.out.println(String) without synchronization, can the output get interleaved? Or is
Multiple part question on running gvim on windows xp. I have a _vimrc file
Multiple NSURLConnections being started (in a single UIViewController) to gather different kinds of data.
multiple webapp running on same tomcat using same jvm. sometime, one webapp that have
Multiple sites have buttons where you can tweet, digg, etc. On this page you'll
Multiple categories selection system for Article Earlier M using two tables articles & categories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.