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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:33:12+00:00 2026-06-09T23:33:12+00:00

I was asked this on an interview this past week, and I didn’t have

  • 0

I was asked this on an interview this past week, and I didn’t have the answer (the correct answer anyways). Say for instance you have list A which has the following elements [1,3,5,7,9,10] and then you have list B, which has the following elements: [3,4,5,6,7], and you want to know which elements in list B are in list A. My answer was:

for item in listA:
    for item1 in listB:
        if item1 == item:
            put item1 in some third list

But I know this is bad, because say listA is one million elements, and listB is a hundred thousand, this solution is just rubbish.

What’s the best way to achieve something like this without iteration of both lists?

  • 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-09T23:33:13+00:00Added an answer on June 9, 2026 at 11:33 pm

    I’d suggest converting them both to sets and doing an intersection:

    setA = set(listA)
    setB = set(listB)
    setA.intersection(setB)
    

    Edit: Note that this will remove any duplicate elements that were in both lists. So if we had listA = [1,1,2,2,3] and listB = [1,1,2,3] then the intersection will only be set([1,2,3]). Also, for a worst-case estimate, this will be as slow as the list comprehension – O(n * m), where n and m are the respective lengths of the lists. Average case is a far better O(n) + O(m) + O(min(m,n)) == O(max(m,n)), however.

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

Sidebar

Related Questions

This was asked in my Google interview recently and I offered an answer which
I was recently asked this question in an interview. Lets suppose I have 2000
Recently I was asked this question in an interview. I gave an answer in
I was asked this question recently during my job interview, and I couldn't answer
I was asked this question in an interview Plz tell me the answer :-
I have been asked this question in interview how does XML and Soap work
I was asked this question in a .net/C# interview: If we have two threads
One of my friend was asked this question in an interview - You have
Got asked this interesting interview question today. Explain in detail the process by which
I was asked this question in interview. Which of the following is better 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.