Python Program to Merge Two Arrays


The process of combining the elements of the given arrays is known as merging. This operation can be done in many ways using many techniques. Let us discuss all techniques that help in merging the given arrays in Python. Before getting into the techniques, let us understand how merging of arrays takes place with a simple Input output scenario.

Input Output Scenario

Consider two arrays arr1 and arr2.

arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
arr2 = [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]

Now, the merged array is the resulting array that we get after merging the arrays arr1 and arr2.

merged_array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ].

Using the “ + ” Operator

The operator “ + ” is used in order to add the values in general mathematics. But, in the case of arrays, its usage is quite different from the rest of the applications. It can be used to combine and merge the arrays which incorporates merging operation.

Syntax

The syntax used for merging the given arrays by using the operator “ + ” is as follows −

merged_array = arr1 + arr2 + arr3 + arr4 + arr5 + arr6 + arr7 + arr8 + . . . . . . . . . . . . . . . . + arrN

Here, arr1, arr2, arr3, arr4, arr5, . . . . . ., arrN are the arrays which are supposed to be merged.

Example

In this example, we are going to discuss about the process of merging the arrays using the “ + ” operator.

arr1 = [1, 2, 3, 4]
arr2 = [5, 6, 7, 8]
arr3 = [9, 10, 11, 12]
arr4 = [13, 14, 15, 16]
arr5 = [17, 18, 19, 20]
arr6 = [21, 22, 23, 24]
arr7 = [25, 26, 27, 28]
merged_array = arr1 + arr2 + arr3 + arr4 + arr5 + arr6 + arr7 
print("The first array is: ")
print(arr1)

print("The second array is: ")
print(arr2)

print("The third array is: ")
print(arr3)

print("The fourth array is: ")
print(arr4)

print("The fifth array is: ")
print(arr5)

print("The sixth array is: ")
print(arr6)

print("The seventh array is: ")
print(arr7)

print("The merged array of the given arrays after performing merge operation: ")
print(merged_array)

Output

The output for the above program is as follows −

The first array is: 
[1, 2, 3, 4]
The second array is:
[5, 6, 7, 8]
The third array is:
[9, 10, 11, 12]
The fourth array is:
[13, 14, 15, 16]
The fifth array is:
[17, 18, 19, 20]
The sixth array is:
[21, 22, 23, 24]
The seventh array is:
[25, 26, 27, 28]
The merged array of the given arrays after performing merge operation:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]

Using the “ Naive ” Approach or “ Naive Technique ”

This technique entirely processes with the help of variables declared within the program. If there are two arrays which must be merged, then a new array is created into which the elements both arrays will be stored. Finally, this array will be considered as the resulting merged array.

Similarly, if three arrays are supposed to be merged, then the elements of all the three arrays will be stored in a newly created fourth array. Let us discuss the algorithm that is followed by this technique and then construct the program.

Example

In the following example, we are going to discuss about the process of merging two or more arrays using Naïve approach.

  • Step 1 − Declare two or more arrays that are desired to be merged.

  • Step 2 − Create a new array into which the elements of the initial arrays can be stored.

  • Step 3 − Traverse all the elements of the initial arrays and store those elements simultaneously into the newly created array.

  • Step 4 − Print the newly created array after traversing all the elements.

def merge_arrays(arr1, arr2, size1, size2, arr3):
   i = 0
   j = 0
   k = 0
   while(i < size1):
      arr3[k] = arr1[i]
      k = k + 1
      i = i + 1
   while(j < size2):
      arr3[k] = arr2[j]
      k = k + 1
      j = j + 1
   arr3.sort()

if __name__ == '__main__':

   arr1 = [1, 3, 5, 7, 9, 11]
   size1 = len(arr1)

   arr2 = [0, 2, 4, 6, 8, 10]
   size2 = len(arr2)
   arr3 = [0 for i in range(size1 + size2)]
   merge_arrays(arr1, arr2, size1, size2, arr3)
   print("The first array before merging is: ")
   print(arr1)
   print("The second array before merging is: ")
   print(arr2)

   print("The array after being merged and sorted: ")
   print(arr3)

Output

The output for the above program is as follows

The first array before merging is: 
[1, 3, 5, 7, 9, 11]
The second array before merging is:
[0, 2, 4, 6, 8, 10]
The array after being merged and sorted:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Updated on: 05-May-2023

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements