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

  • Home
  • SEARCH
  • 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 5841105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:50:45+00:00 2026-05-22T11:50:45+00:00

I have 2 ArrayLists that have a Array of Strings as a component. I

  • 0

I have 2 ArrayLists that have a Array of Strings as a “component”. I want to find the “components” whose first element is the same in both ArrayLists.
To be more clear:

ArrayList One
first component => {"0", "zero"}
second component => {"1", "one"}
ArrayList Two
first component => {"1", "uno"}
second component => {"2", "two"}

I would like to loop through ArrayList Two and find {“1″,”uno”}.
So far I have a nested loop that loops through the first array and then checks the current component to each component in ArrayList Two.

    for(int i=0; i<One.size(); i++)
    {
        for(int j=0; j<Two.size(); j++)
        {
            if( fileOne.get(i)[0].equals( Two.get(j)[0] ) )
            {
                System.out.print( Two.get(j)[0]+" " );
                System.out.print( Two.get(j)[1] );
                System.out.println();
            }
        }
    }

I think there should be a better solution. Any 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-05-22T11:50:45+00:00Added an answer on May 22, 2026 at 11:50 am

    Use a HashSet.

    Set<String> set = new HashSet<String>()
    for(int i=0; i<One.size(); i++) {
      set.add(fileOne.get(i)[0]);
    }
    
    for(int i=0; i<Two.size(); i++) {
      String component[] = Two.get(j)
      if(set.contains(component[0])) {
        System.out.print( component[0]+" " );
        System.out.print( component[1] );
        System.out.println();
       }
    }
    

    Note: A List would not work in this case, because lookups in Lists are O(N). Lookups in HashSets are O(1), so building the set (first loop) is O(N). Then going through your second array is O(M) and each lookup is O(1).

    Overall, this becomes O(N) + ( O(M) * O(1) ) = O(N+M)

    Edit: for Ted’s comments.

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

Sidebar

Related Questions

I have an array list that contains Quote objects. I want to be able
I have an ArrayList<String> , and I want to remove repeated strings from it.
I have a string array or arraylist that is passed to my program in
Ok So i have made my array and added an action listener so that
I have an arraylist that contains items called Room. Each Room has a roomtype
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
In Java, say you have a class that wraps an ArrayList (or any collection)
I need to make an ArrayList of ArrayLists thread safe. I also cannot have
I have some data stored as ArrayList . And when I want to backup
I have an array of different type objects and I use a BinaryWriter to

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.