Click here to Skip to main content
15,358,528 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Find the intersection of two sorted lists, i.e., find the elements that occur in both.

Input: 2 sorted lists
Output: A single sorted list that contains the elements present in both the arrays.

Input 1:
[1, 2, 3, 5, 9]
[3, 6, 9, 12]
Output 1:
[3, 9]

Input 2:
[1, 1, 2, 4, 8, 8, 8, 9]
[1, 2, 6, 7, 8, 8, 8]
Output 2:
[1, 2, 8, 8, 8]

What I have tried:

# Reading the input
import ast 
input_lists = ast.literal_eval(input())
m = input_lists[0]
n = input_lists[1]
def union_array(arr1, arr2):
    m = len(arr1)
    n = len(arr2)
    i = 0
    j = 0
    # keep track of last element to avoid duplicates
    prev = None
    while i < m and j < n:
        if arr1[i] < arr2[j]:
            if arr1[i] != prev:
                print(arr1[i], end=' ')
                prev = arr1[i]
            i += 1
        elif arr1[i] > arr2[j]:
            if arr2[j] != prev:
                print(arr2[j], end=' ')
                prev = arr2[j]
            j += 1
            if arr1[i] != prev:
                print(arr1[i], end=' ')
                prev = arr1[i]
            i += 1
            j += 1
    while i < m:
        if arr1[i] != prev:
            print(arr1[i], end=' ')
            prev = arr1[i]
        i += 1
    while j < n:
        if arr2[j] != prev:
            print(arr2[j], end=' ')
            prev = arr2[j]
        j += 1
Updated 21-May-22 3:43am
0x01AA 21-May-22 7:56am
Richard MacCutchan 21-May-22 8:30am
That is a solution I would think.
0x01AA 21-May-22 8:38am
Thanks, done. I hope OP has no questions for details because I don't know Python ;)
Mahi Choudhary 21-May-22 8:47am
no, this is not the solution, This is method that I have tried but unable to get the correct output
Mahi Choudhary 21-May-22 8:46am
yes I have but unable to get the correct output
0x01AA 21-May-22 8:50am
Best is to update your question with the current code and show what you expect and what you get.
Richard MacCutchan 21-May-22 8:50am
You just need to search the first list in a linear fashion. Each time you find a matching number in the second list you should remove it, and restart the search of the second array. That way you should be able to find any multiple numbers, such as the 8.
Mahi Choudhary 21-May-22 8:52am
thanks, I found the correct way
Richard MacCutchan 21-May-22 8:55am
Then please post your code and mark it as solved.

Your approach, doing it from scratch is pretty ok to learn/train programming.

In praxis it is usually better to use the tools the language already supports. This because they are tested and you can rely on them.

Therefore I suggest to have a look here: Python | Intersection of two lists - GeeksforGeeks[^]

I'm aware that the above link does not give exactly the solution you are looking for. The idea is more, to show you some powerfull technics supported by the language itself which will maybe help to get a solution more easy.

Are you sure this code is really an answer to this question ?
To me it look like a merge of 2 sorted lists without repeats.

How do I solve this below question

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900