Open In App

C Program To Merge Two Arrays

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
1 3 5 2 4 6 

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;
}

Output
1 3 5 2 4 6 

Time Complexity: O (n1 + n2), where n1 and n2 are sizes of given arrays respectively.
Auxiliary Space: O (n1 + n2)



Next Article

Similar Reads