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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:48:05+00:00 2026-05-24T09:48:05+00:00

I have 2 lists of integers, l1 = new ArrayList(); l2 = new ArrayList();

  • 0

I have 2 lists of integers,

l1 = new ArrayList();
l2 = new ArrayList();

I want to find out duplicate items in both of them, I have my usual approach:-

for (Integer i : l1)
{
 if(l2.contains(i)){
    System.out.println("Found!");
  } 
}

I’ve heard contains() is O(n), making my implementation O(n^2).

Is there a better way to do this, (less than O(n^2)) ?

  • 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-24T09:48:07+00:00Added an answer on May 24, 2026 at 9:48 am

    Sure – create a HashSet<Integer> from one of the lists first.

    Set<Integer> set = new HashSet<Integer>(l2);
    for (Integer i : l1) {
        if (set.contains(i)) {
            System.out.println("Found!");
        }
    }
    

    If you want to find all duplicate entries, you don’t even need to write your own loop, as Set<E> provides everything you need…

    Set<Integer> set = new HashSet<Integer>(l2);
    set.retainAll(new HashSet<Integer>(l1));
    

    Afterwards, set will be the intersection of the two lists.

    Note that you can be even more efficient than this if both your lists are already sorted to start with. You just iterate over both at the same time, moving the “cursor” forward for whichever iterator currently has the smaller value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i have a List<Integer> l = new ArrayList<Integer>(); and would like to have
I have some ArrayList s declared. Some of them are ArrayList<Integer> and other are
here is what i have got ArrayList<Integer> list = new ArrayList<Integer>(); Integer a =
I have an ArrayList, which includes a number of items I want to remove.
I have a list of integers, List<Integer> and I'd like to convert all the
If I have a simple list of Strings: List<String> stringList = new ArrayList<String>(); I
I want to find the subsets of a set of integers. It is the
Basically I have an integer array which i want to hand over to a
If I have a Map like this: HashMap<Integer, ComparableObject> map; and I want to
I have a list of integers in C#. I wish to remove duplicates. In

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.