Merging Arrays 


 

Merging involves taking two arrays in which the values are ordered in some fashion, and producing a third array that contains the elements of both arrays ordered in the same fashion, with duplicates removed.

═════════════════════════

Pseudocode:

While index1 and index2 are both OK
    processPair   
    indexTest
End While
FinishUp


processPair:

If array1 (index1) < array2 (index2) Then
    array3 (index3) = array1 (index1)
    index1 = index1 + 1
    index3 = index3 + 1
ElseIf array1 (index1) > array2 (index2) Then
    array3 (index3)  = array2 (index2)
    index2 = index2 + 1
    index3 = index3 + 1
Else ' array1 (index1) = array2 (index2)
    array3 (index3) = array1 (index1)
    index1 = index1 + 1
    index2 = index2 + 1
    index3 = index3 + 1
EndIf

   


indexTest:

  1. array1 and array2 both still have more items.
  2. array1 and array2 are both are finished.
  3. array1 is finished but array2 still has more items.
  4. array2 is finished but array1 still has more items.

If index1 <= array1.GetUpperBound(0) And index2 <= array2.GetUpperBound(0) Then
       BothOK = True
ElseIf index1 > array1.GetUpperBound(0) And index2 > array2.GetUpperBound(0) Then
       BothOUT = True 
     
ElseIf index1 > array1.GetUpperBound(0) Then
        array1OUT = True
Else ' index2 > array2.GetUpperBound(0)
        array2OUT = True
EndIf


FinishUp:

If array1OUT Then
        For counter = index2 to array2.GetUpperBound(0)
                array3 (index3) = array2 (counter)
                index3 = index3 + 1
        Next counter
ElseIf array2Out Then
        For counter = index1 to array1.GetUpperBound(0)
                array3 (index3) = array1 (counter)
                index3 = index3 + 1
        Next counter
EndIf


Complete Version:

index1 = array1.GetLowerBound(0)
index2 = array2.GetLowerBound(0)
index3 = array3.GetLowerBound(0)

While BothOK 
    ' processPair 

If array1 (index1) < array2 (index2) Then
    array3 (index3) = array1 (index1)
    index1 = index1 + 1
    index3 = index3 + 1
ElseIf array1 (index1) > array2 (index2) Then
    array3 (index3)  = array2 (index2)
    index2 = index2 + 1
    index3 = index3 + 1
Else 'array1 (index1) = array2 (index2)
    array3 (index3) = array1 (index1)
    index1 = index1 + 1
    index2 = index2 + 1
    index3 = index3 + 1
EndIf

    ' indexTest

If index1 <= array1.GetUpperBound(0) And _
   index2 <= array2.GetUpperBound(0) Then
       BothOK = True
ElseIf index1 > array1.GetUpperBound(0) And _
          index2 > array2.GetUpperBound(0) Then
       BothOUT = True
     
ElseIf index1 > array1.GetUpperBound(0) Then
        array1OUT = True
Else ' index2 > array2.GetUpperBound(0)
        array2OUT = True
EndIf

End While

'FinishUp

If array1OUT Then
        For counter = index2 to array2.GetUpperBound(0) 
                array3 (index3) = array2 (counter)
                index3 = index3 + 1
        Next counter
ElseIf array2Out Then
        For counter = index1 to array1.GetUpperBound(0)  
                array3 (index3) = array1 (counter)
                index3 = index3 + 1
        Next counter
EndIf


Demo program