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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:34:02+00:00 2026-06-13T21:34:02+00:00

Possible Duplicate: Common elements in two lists I am trying to take 2 lists

  • 0

Possible Duplicate:
Common elements in two lists

I am trying to take 2 lists of integers, and search the lists to find elements which are the same. A new list will then be created with all common elements. I am able to get it to find common elements at the same position but not at different positions. My code can be viewed below:

class Share {


public static void main(String[] args) {
    ArrayList<Integer> oneList = new ArrayList<Integer>();
    ArrayList<Integer> twoList = new ArrayList<Integer>();

    oneList.add(8);
    oneList.add(2);
    oneList.add(5);
    oneList.add(4);
    oneList.add(3);

    twoList.add(1);
    twoList.add(2);
    twoList.add(3);
    twoList.add(4);
    twoList.add(5);

    System.out.println(sharedItems(oneList, twoList));
}

static List<Integer> sharedItems(List<Integer> list1, List<Integer> list2) {
    Iterator<Integer> it1 = list1.iterator();
    Iterator<Integer> it2 = list2.iterator();
    int i1 = 0;
    int i2 = 0;
    ArrayList<Integer> shareList = new ArrayList<Integer>();

    while (it1.hasNext()){
        i1 = it1.next();}
    System.out.println(i1);
     while (it2.hasNext()){
            i2 = it2.next();
            if (i1 == i2){
                shareList.add(i1);
        }
    }
    return shareList;
}



}
  • 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-13T21:34:03+00:00Added an answer on June 13, 2026 at 9:34 pm

    You could use a Set instead:

    Set<Integer> commonIntegers = new HashSet<Integer>(list1).retainAll(list2);
    

    You can then either change the signature of your method and return the set as is:

    static Collection<Integer> sharedItems(...) {
        return new HashSet<Integer>(list1).retainAll(list2);
    }
    

    or wrap it in a list:

    return new ArrayList<Integer> (commonIntegers);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Common elements comparison between 2 lists I have two Lists: [Apples, Bananas,
Possible Duplicate: Find common values in multiple arrays with PHP I'm trying to find
Possible Duplicate: Comparing two arrays & get the values which are not common I
Possible Duplicate: How can I find the common ancestor of two nodes in a
Possible Duplicate: How to find the nearest common ancestors of two or more nodes?
Possible Duplicate: Reference unit tests for common data structures? I'm trying to implement the
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to find ancestor-or-self that is a child of an element with
Possible Duplicate: Where to put common writable application files? In my application I have
Possible Duplicate: Most common or vicious mistakes in C# development for experienced C++ programmers

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.