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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:08:29+00:00 2026-06-12T17:08:29+00:00

public static <E> ArrayList<E> union (ArrayList<E>array1, ArrayList<E>array2) { //arrayUnion will be the arrayList that

  • 0
public static <E> ArrayList<E> union
        (ArrayList<E>array1, ArrayList<E>array2)
{
  //arrayUnion will be the arrayList that will be returned
  ArrayList <E> arrayUnion  = new ArrayList <E>(array1);   
  arrayUnion.addAll(array2);
  E current;

  for(int i = 0; i < arrayUnion.size(); i++)
  {
      for(int j = 0; j < arrayUnion.size(); j++)
      {
          current = arrayUnion.get(i);
          if(current.equals(arrayUnion.get(j)))
          {
              arrayUnion.remove(j);
          }
      }
  }

      return arrayUnion;
}

For my test of this method, this was the output:

The first list is [ww, ee, rr, t, yy]

The second list is [ww, ss, ee, dd]

The union of both ArrayLists is: [ee, t, ww, dd]

What went wrong..? I’ve been stuck on this for far too long and I never want to hear the word Union again. Plz help

  • 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-12T17:08:30+00:00Added an answer on June 12, 2026 at 5:08 pm

    Your code has to do check the current item is checked with itself. If not you have to remove the item and decrease the j with one as you have to check again the item replaced at j. I have modified your code to work for your case. Just look at the condition check for item to be removed.

    public static <E> ArrayList< E > union( ArrayList< E > array1, ArrayList< E > array2 ) {
        // arrayUnion will be the arrayList that will be returned
        ArrayList< E > arrayUnion = new ArrayList< E >( array1 );
        arrayUnion.addAll( array2 );
        E current;
    
        for ( int i = 0; i < arrayUnion.size( ); i++ ) {
    
            for ( int j = 0; j < arrayUnion.size( ); j++ ) {
                current = arrayUnion.get( i );
    
                if ( i != j && current.equals( arrayUnion.get( j ) ) ) {
                    arrayUnion.remove( j );
                    --j;// This is set to check the item which replace the removed item at previous statement
                }
            }
        }
    
        return arrayUnion;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static ArrayList GetStudentAsArrayList() { ArrayList students = new ArrayList { new Student() {
public static UserFriendListContainer getFriendList(SoapObject wizardobject)//TESTED { UserFriendListContainer container= new UserFriendListContainer(); List<UserFriendListModel> list= new ArrayList();
public class ProductListGetSet { static ArrayList<ProductListItems> productListItems; ProductListItems items; ProductListGetSet() { productListItems = new
I am trying do something like this:- public static ArrayList<myObject>[] a = new ArrayList<myObject>[2];
class A contain: public static ArrayList<String> sourceList = new ArrayList<String>(); // here ArrayList contain
public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
public static void main(String Data[]) { ConnectionPoolDataSource dps; try { cnt=new InitialContext(); cnt.rebind(java:comp/env/jdbc/pool/dragon, dps);
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
I end up with all the deliveries in the following array public static ArrayList
Is any way to put this into Bundle during onSaveInstanceState ? public static ArrayList<StringProArrayList>

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.