Skip to content

Commit

Permalink
Merge pull request #169 from Abhi-sheKkK/union_of_two_lists
Browse files Browse the repository at this point in the history
Union of two lists
  • Loading branch information
abhisek247767 authored Oct 31, 2024
2 parents 113b9ef + e8a24ee commit e56c977
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions python/union_of_two_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'''
Question : We have been given two sorted lists . We have to result a new list which is sorted
and also the union of the two given lists .
Example : input a = [1,1,2,3,4,5] , b= [1,2,7,8]
Output r=[1,2,3,4,5,7,8]
Approach : We will use two pointers method for this , i for list a and j for list b.
If a[i] is less than b[j], add a[i] to the result if it's not already added, then move i.
If a[i] is greater than b[j], add b[j] to the result if it's not already added, then move j.
If a[i] equals b[j], add one of them and move both pointers.
After one of the arrays is exhausted, add the remaining unique elements from the other array.
The result will be the sorted union of the two arrays with unique elements.
'''

class Solution:

#Function to return a list containing the union of the two arrays.
def findUnion(self,a,b):
# code here
i, j = 0, 0
result = []

# Traverse both arrays
while i < len(a) and j < len(b):
# If the current element in a[] is smaller, add it to the result if unique and move pointer i
if a[i] < b[j]:
if len(result) == 0 or result[-1] != a[i]:
result.append(a[i])
i += 1
# If the current element in b[] is smaller, add it to the result if unique and move pointer j
elif a[i] > b[j]:
if len(result) == 0 or result[-1] != b[j]:
result.append(b[j])
j += 1
# If both elements are equal, add only one of them if unique and move both pointers
else:
if len(result) == 0 or result[-1] != a[i]:
result.append(a[i])
i += 1
j += 1

# Add remaining elements from a[]
while i < len(a):
if result[-1] != a[i]:
result.append(a[i])
i += 1

# Add remaining elements from b[]
while j < len(b):
if result[-1] != b[j]:
result.append(b[j])
j += 1

return result

0 comments on commit e56c977

Please sign in to comment.