To find the union of two sorted arrays, you can use a similar approach as finding the intersection of two sorted arrays. Here’s an algorithm to find the union of two sorted arrays:
- Initialize two pointers
iandjto 0, which will be used to traverse the two sorted arrays. - Create an empty list
resultto store the union of the two arrays. - While both pointers
iandjare within the bounds of the arrays, do the following:- If
arr1[i]is less thanarr2[j], addarr1[i]to theresultlist and incrementi. - If
arr1[i]is greater thanarr2[j], addarr2[j]to theresultlist and incrementj. - If
arr1[i]is equal toarr2[j], add eitherarr1[i]orarr2[j]to theresultlist and increment both pointers.
- If
- If there are any remaining elements in either array after the pointers have reached the end of the arrays, add those elements to the
resultlist. - Convert the
resultlist to an array and return it.
The time complexity of this algorithm is O(m + n), where m and n are the lengths of the two arrays.
Here’s an example implementation of the above algorithm in Kotlin:
fun union(arr1: IntArray, arr2: IntArray): IntArray {
val result = mutableListOf<Int>()
var i = 0
var j = 0
while (i < arr1.size && j < arr2.size) {
when {
arr1[i] < arr2[j] -> {
result.add(arr1[i])
i++
}
arr1[i] > arr2[j] -> {
result.add(arr2[j])
j++
}
else -> {
result.add(arr1[i])
i++
j++
}
}
}
while (i < arr1.size) {
result.add(arr1[i])
i++
}
while (j < arr2.size) {
result.add(arr2[j])
j++
}
return result.toIntArray()
}
fun main(){
val arr1 = intArrayOf(1, 2, 3, 4, 5, 6)
val arr2 = intArrayOf(4, 5, 6, 7, 8)
println(union(arr1, arr2).contentToString()) // Output: [1, 2, 3, 4, 5, 6, 7, 8]
}To find the union of two sorted arrays:
- Initialize two pointers
iandjto 0, which will be used to traverse the two sorted arrays.This step sets up two pointers that will be used to track the current position in each of the two input arrays. - Create an empty list
resultto store the union of the two arrays.Theresultlist will be used to store the elements that are present in either or both of the input arrays. - While both pointers
iandjare within the bounds of the arrays, do the following:This step sets up a loop that will run until we have reached the end of both arrays. - If
arr1[i]is less thanarr2[j], addarr1[i]to theresultlist and incrementi.If the element at the current position inarr1is smaller than the element at the current position inarr2, it means that the element atarr1[i]is not present inarr2, so we add it to theresultlist. We then increment theipointer to move to the next element inarr1. - If
arr1[i]is greater thanarr2[j], addarr2[j]to theresultlist and incrementj.If the element at the current position inarr1is greater than the element at the current position inarr2, it means that the element atarr2[j]is not present inarr1, so we add it to theresultlist. We then increment thejpointer to move to the next element inarr2. - If
arr1[i]is equal toarr2[j], add eitherarr1[i]orarr2[j]to theresultlist and increment both pointers.If the element at the current position inarr1is equal to the element at the current position inarr2, it means that the element is present in both arrays, so we can add eitherarr1[i]orarr2[j]to theresultlist. We then increment both pointers to move to the next elements in both arrays. - If there are any remaining elements in either array after the pointers have reached the end of the arrays, add those elements to the
resultlist.This step handles the case where we have reached the end of one of the input arrays but there are still elements remaining in the other array. In this case, we can simply add the remaining elements to theresultlist. - Convert the
resultlist to an array and return it.This step converts theresultlist to anIntArrayand returns it.
Overall, the algorithm works by comparing the elements at the current positions in both input arrays and adding the smaller of the two elements to the result list. If the elements are equal, we add either one to the result list. We then increment the pointers for the arrays containing the smaller element. This process continues until we reach the end of both arrays. Any remaining elements in either array are added to the result list. The resulting IntArray contains all the elements that are present in either or both of the input arrays.