C Program To Merge Two Arrays
Last Updated :
30 Aug, 2024
Merging two arrays means combining/concatenating the elements of both arrays into a single array.
Example
Input: arr1 = [1, 3, 5], arr2 = [2, 4, 6]
Output: res = [1, 3, 5, 2, 4, 6]
Explanation: The elements from both arrays are merged into a single array.
Input: arr1 = [10, 40, 30], arr2 = [15, 25, 5]
Output: res = [10, 40, 30, 15, 25, 5]
Explanation: Elements from both arrays are merged into a single array.
Note: This article doesn't consider the order of the array. If you want to merge two sorted arrays into a sorted one, refer to this article - Merge two sorted arrays
Using memcpy()
Simplest method to merge two arrays is to create a new array large enough to hold all elements from both input arrays. Copy elements from both arrays into the new array using memcpy().
C
// C program to merge two arrays into a new array using
// memcpy()
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int* mergeArrays(int arr1[], int n1, int arr2[], int n2) {
// Resultant array to store merged array
int *res = (int*)malloc(sizeof(int) * n1 * n2);
// Copy elements of the first array
memcpy(res, arr1, n1 * sizeof(int));
// Copy elements of the second array
memcpy(res + n1, arr2, n2 * sizeof(int));
return res;
}
int main() {
int arr1[] = {1, 3, 5};
int arr2[] = {2, 4, 6};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
// Merge arr1 and arr2
int* res = mergeArrays(arr1, n1, arr2, n2);
for (int i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);
return 0;
}
Time Complexity: O (n1 + n2), where n1 and n2 are sizes of given arrays respectively.
Auxiliary Space: O (n1 + n2)
Manually using Loops
Use a loop to iterate the first array and copy the elements to the new array one by one. Then copy the elements of the second array to new array but start from the index next to the last elopement of the first array.
C
// C program to merge two arrays into a new array
#include <stdio.h>
#include <stdlib.h>
int* mergeArrays(int arr1[], int n1, int arr2[],int n2) {
// Allocating array for storing result
int *res = (int *)malloc((n1 + n2) * sizeof(int));
// Copy elements of the first array to the result array
for (int i = 0; i < n1; i++)
res[i] = arr1[i];
// Copy elements of the second array to the result array
for (int i = 0; i < n2; i++)
res[n1 + i] = arr2[i];
return res;
}
int main() {
int arr1[] = {1, 3, 5};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int arr2[] = {2, 4, 6};
int n2 = sizeof(arr2) / sizeof(arr2[0]);
// Merge the two arrays
int *res = mergeArrays(arr1, n1, arr2, n2);
for (int i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);
return 0;
}
Time Complexity: O (n1 + n2), where n1 and n2 are sizes of given arrays respectively.
Auxiliary Space: O (n1 + n2)
Similar Reads
Array C/C++ Programs C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N
6 min read
C Program to Find Common Array Elements Here, we will see how to find the common array elements using a C program. Input: a[6] = {1,2,3,4,5,6} b[6] = {5,6,7,8,9,10} Output: common array elements is 5 6 C // C program to find the common array elements #include <stdio.h> int main() { int a[6] = { 1, 2, 3, 4, 5, 6 }; int b[6] = { 5, 6,
1 min read
C Program for Iterative Merge Sort Following is a typical recursive implementation of Merge Sort that uses last element as pivot. C /* Recursive C program for merge sort */ #include <stdio.h> #include <stdlib.h> /* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */ void merge(int arr[], int l, int
3 min read
C Program to Find Common Array Elements Between Two Arrays Here we will build a C Program to Find Common Array Elements between Two Arrays. Given two arrays we have to find common elements in them using the below 2 approaches: Using Brute forceUsing Merge Sort and then Traversing Input: array1[] = {8, 2, 3, 4, 5, 6, 7, 1} array2[] = {4, 5, 7, 11, 6, 1} Outp
4 min read
C Program to Copy an Array to Another Array In this article, we will learn how to copy all the elements of one array to another array in C.The simplest method to copy an array is by using the memcpy() function. Let's take a look at an example:C#include <stdio.h> #include <string.h> int main() { int arr1[] = {1, 2, 3, 4, 5}; int n
3 min read