How can I implement an algorithm for finding the intersection of lists consisting of integers? Through the set I tried, but then does not display duplicate elements. I know that it is possible to go through the lists with nested loops and output the same elements, but I cannot implement it. It is necessary that the complexity of the algorithm is O (len (list1) + len (list2)) . If anyone helps, I will be grateful. Example:
list1 = [1,3,7,9,10] list2 = [2,3,4,7,8,11] Answer: [3, 7]
An example with repetitions:
list1 = [1,3,3,4,6] list2 = [3,3,3,6,8,9] then should withdraw [3,3,6] .