I have the following problem: I need to find pairs of identical elements in two lists that are unordered. The point in these two lists is that they are "approximately equal" - only some elements are shifted by several indices, for example. (Note, these objects are not ints, I just use 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 was to iterate over both lists and generate two HashMaps based on some unique key for each object. Then, from the second pass, I just pulled out the elements from both cards. This gives O(2N) in space and time.
I was thinking of a different approach: we will point to pointers to the current element in both lists, as well as to the current set for each list. pseudocode will look like this:
while(elements to process) elem1 = list1.get(index1) elem2 = list2.get(index2) if(elem1 == elem2){
The above probably does not work ... I just wanted to get a general idea of ββwhat you think of this approach. Basically, it supports a hash on the side of each list whose size is <size problem. This should be ~ O (N) for a small number of non-local elements and for small "gaps". Anyway, I look forward to your answers.
EDIT: I can't just return the intersection set of two lists of objects, since I need to perform operations (even multiple operations) for the objects that I find as a match / mismatch