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.ExampleInput: 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 arraysUsing 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; } Output1 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 LoopsUse 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; } Output1 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) Comment More infoAdvertise with us Next Article C Program To Merge Two Arrays L laxmigangarajula03 Follow Improve Article Tags : C Programs C Language C Array Programs 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 Array of Pointers to Strings in C In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings 2 min read C Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l 3 min read C program to append content of one text file to another Pre-requisite: File Handling in C Given the source and destination text files, the task is to append the content from source file to destination file and then display the content of the destination file.Examples: Input: file1.text This is line one in file1 Hello World. file2.text This is line one in 2 min read C Program to Concatenate Two Strings Using a Pointer Concatenating two strings means appending one string at the end of another string. While the standard library provides strcat() for concatenation, this article will demonstrate how to concatenate two strings using pointers.To concatenate two strings using pointers, traverse the first string to its n 1 min read How to Initialize Array to 0 in C? Initializing an array to zero is a common practice in programming to ensure that all elements start with a known value. In C, there are several ways to initialize an array to zero. In this article, we will explore different methods to do so. Initialize Array to 0 in CThere are mainly two ways to ini 2 min read Like