Skip to content

Commit 6b74cf0

Browse files
committed
Union of two unsorted array with duplicate elements using two pointers
1 parent 579bae8 commit 6b74cf0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
arr1 = [1, 2, 3, 4, 5, 5, 5]
2+
arr2 = [1, 1, 2, 2, 5, 5, 6, 6, 6, 4, 4, 4]
3+
arr1.sort()
4+
arr2.sort()
5+
result = []
6+
i = 0
7+
j = 0
8+
9+
while i < len(arr1) and j < len(arr2):
10+
11+
if arr1[i] <= arr2[j]:
12+
if len(result) == 0 or (len(result) and result[-1] != arr1[i]):
13+
result.append(arr1[i])
14+
i += 1
15+
elif arr1[i] > arr2[j]:
16+
if len(result) == 0 or (len(result) and result[-1] != arr2[j]):
17+
result.append(arr2[j])
18+
j += 1
19+
20+
while i < len(arr1):
21+
if len(result) and result[-1] != arr1[i]:
22+
result.append(arr1[i])
23+
i += 1
24+
25+
while j < len(arr2):
26+
if len(result) and result[-1] != arr2[j]:
27+
result.append(arr2[j])
28+
j += 1
29+
30+
print(result)

0 commit comments

Comments
 (0)