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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:28:13+00:00 2026-06-12T12:28:13+00:00

I have the following problem: I need to find pairs of the same elements

  • 0

I have the following problem: I need to find pairs of the same elements in two lists, which are unordered. The thing about these two lists is that they are “roughly equal” – only certain elements are shifted by a few indexes e.g. (Note, these objects are not ints, I am just using integers in this example):

[1,2,3,5,4,8,6,7,10,9]
[1,2,3,4,5,6,7,8,9,10]

My first attempt would be to iterate through both lists and generate two HashMaps based on some unique key for each object. Then, upon the second pass, I would simply pull the elements from both maps. This yields O(2N) in space and time.

I was thinking about a different approach: we would keep pointers to the current element in both lists, as well as currentlyUnmatched set for each of the list. the pseudocode would be sth of the following sort:

while(elements to process)
    elem1 = list1.get(index1)
    elem2 = list2.get(index2)
    if(elem1 == elem2){ //do work
         ... index1++; 
             index2++;
    }
    else{
        //Move index of the list that has no unamtched elems
        if(firstListUnmatched.size() ==0){
            //Didn't find it also in the other list so we save for later 
            if(secondListUnamtched.remove(elem1) != true)
                firstListUnmatched.insert(elem1)
            index1++
        }
        else { // same but with other index}
    }

The above probably does not work… I just wanted to get a rough idea what you think about this approach. Basically, this maintains a hashset on the side of each list, which size << problem size. This should be ~O(N) for small number of misplaced elements and for small “gaps”. Anyway, I look forward to your replies.

EDIT: I cannot simply return a set intersection of two object lists, as I need to perform operations (multiple operations even) on the objects I find as matching/non-matching

  • 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-12T12:28:14+00:00Added an answer on June 12, 2026 at 12:28 pm

    I cannot simply return a set intersection of two object lists, as I need to perform operations (multiple operations even) on the objects I find as matching/non-matching

    You can maintain a set of the objects which don’t match. This will be O(M) in space where M is the largest number of swapped elements at any point. It will be O(N) for time where N is the number of elements.

    interface Listener<T> {
        void matched(T t1);
        void onlyIn1(T t1);
        void onlyIn2(T t2);
    }
    
    public static <T> void compare(List<T> list1, List<T> list2, Listener<T> tListener) {
        Set<T> onlyIn1 = new HashSet<T>();
        Set<T> onlyIn2 = new HashSet<T>();
        for (int i = 0; i < list1.size(); i++) {
            T t1 = list1.get(i);
            T t2 = list2.get(i);
            if (t1.equals(t2)) {
                tListener.matched(t1);
                continue;
            }
            if (onlyIn2.remove(t1)) 
                tListener.matched(t1);
             else 
                onlyIn1.add(t1);
            if (!onlyIn1.remove(t2))
                onlyIn2.add(t2);
        }
        for (T t1 : onlyIn1)
            tListener.onlyIn1(t1);
        for (T t2 : onlyIn2)
            tListener.onlyIn2(t2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem, on which I can't seem to find an answer...
I have the following problem: We need to find the next august. I other
I have the following problem: I need to use XSLFO to generate a 2-column
Hi i have following Problem. I write a Mediawiki Extension where i need some
I have a problem with Matlab - I need to do the following :
I have following problem: I have to make a ASP.NET Webapplication with a two-row
I have the following problem. I need to organize automatic upload to deploy server
I am new in xslt. I have the following problem. I need within an
I have the following problem: I have a form I need to serialize but
I have the following problem - I'm catching a key event an I need

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.