VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-merge-two-arrays/

⇱ C Program To Merge Two Arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program To Merge Two Arrays

Last Updated : 23 Jul, 2025

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().


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.


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)

Comment
Article Tags: