Sobes.tech
Intern

There are two sorted lists (arrays). # You need to write a function that creates a new sorted list with the union. # 1st list: 1, 2, 2, 5, 7, 14 # 2nd list: 2, 2, 2, 4, 6, 6, 7, 9, 14, 15 # answer: 1, 2, 2, 2, 4, 5, 6, 6, 7, 9, 14, 15 def union(arr1: list[int], arr2: list[int]) -> list[int]: pass --- def union(arr1: list[int], arr2: list[int]) -> list[int]: result = [] i = 0 j = 0 while i < len(arr1) and j < len(arr2): if arr1[i] < arr2[j]: result.append(arr1[i]) i += 1 elif arr1[i] > arr2[j]: result.append(arr2[j]) j += 1 else: result.append(arr1[i]) result.append(arr2[j]) i += 1 j += 1 result.extend(arr1[i:]) result.extend(arr2[j:]) return result