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 1025835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:59:23+00:00 2026-05-16T11:59:23+00:00

I want to implement the union, intersect, difference and reverse operations in Java. First

  • 0

I want to implement the union, intersect, difference and reverse operations in Java.

First I have 2 instances of ArrayList<Integer>

a = [0,2,4,5,6,8,10]
b = [5,6,7,8,9,10]

a union b should return c = [0,2,4,5,6,7,8,9,10]

a intersect b should return c = [5,8,10]

a difference b should return c = [0,2,4]

reverse a = [10,8,6,5,4,2,0]

Something like this.

How to implement that method in Java?


Note that I have to start with the following template:

package IntSet;
import java.util.ArrayList;
import java.util.Collection;


public class IntSet {
    
    private ArrayList<Integer> intset;
    
    public IntSet(){
        intset = new ArrayList<Integer>();
    }
    
    public void insert(int x){
        intset.add(x);
    }
    
    public void remove(int x){
        //implement here
        intset.indexOf(x);
    }
    
    public boolean member(int x){
        //implement here
        return true;
    }
    
    public IntSet intersect(IntSet a){
        //implement here
        return a;
    }
    
    public IntSet union(IntSet a){
        //implement here
        return a;
    }
    
    public IntSet difference(IntSet a){
        //implement here
        IntSet b = new IntSet();
        return b; 
    }
}
  • 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-05-16T11:59:23+00:00Added an answer on May 16, 2026 at 11:59 am
    //Union 
    List<Integer> c = new ArrayList<Integer>(a.size() + b.size());
    addNoDups(c,a);
    addNoDups(c,b);
    
    private void addNoDups(List<Integer> toAddTo,List<Integer> iterateOver) {
        for(Integer num:iterateOver){
            if(toAddTo.indexOf(num) == -1) {
                toAddTo.add(num);
            }
        }
    }
    
    //intersection
    List<Integer> c = new ArrayList<Integer> (a.size() > b.size() ?a.size():b.size());
    c.addAll(a);
    c.retainAll(b);
    
    //difference a-b
    List<Integer> c = new ArrayList<Integer> (a.size());
    c.addAll(a);
    c.removeAll(b);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i starter in jqgrid, i want implement inline edit in jqgrid i have this
i have question for example i want to implement binary tree with array i
I want to implement simple SQL query that will return a sorted list. The
I want to implement UITableView Where I want to have 3 buttons in each
I have WSDL and XSD files and want implement a webservice based on this
i want implement geolocation notification like the app reminders. this is what i have
I have on top entity with many child entities. Now, I want implement FindAll()
I want implement in my software solution an VBA editor but in c# 3.0.
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it

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.