Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program to Find Common Array Elements
Next article icon

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
C Program to Find Common Array Elements

L

laxmigangarajula03
Improve
Article Tags :
  • C Language
  • C Programs
  • C Array Programs

Similar Reads

  • C Program to Swap Two Numbers
    Swapping two numbers means exchanging their values. In this article, we will learn how to swap values of two numbers in a C program. The easiest method to swap two numbers is to use a temporary variable. First, we assign the value of first variable to temporary variable, then assign the value of sec
    2 min read
  • 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 Implement Queue using Array
    A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle which means the elements added first in a queue will be removed first from the queue. In this article, we will learn how to implement a Queue using Array in C. Implement Queue using Array in CTo implement Queue u
    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++ Code // 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]
    1 min read
  • C Program to Sort an Array in Ascending Order
    Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element. The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:
    3 min read
  • C Program for Iterative Merge Sort
    Following is a typical recursive implementation of Merge Sort that uses last element as pivot. C/C++ Code /* 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[], i
    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: [GFGTABS] C #include <stdio.h> #include <string.h> int main() { int arr1[] = {1, 2, 3,
    3 min read
  • C Program to Find the closest pair from two sorted arrays
    Write a C program for a given two arrays arr1[0...m-1] and arr2[0..n-1], and a number x, the task is to find the pair arr1[i] + arr2[j] such that absolute value of (arr1[i] + arr2[j] - x) is minimum. Example: Input: ar1[] = {1, 4, 5, 7}; ar2[] = {10, 20, 30, 40}; x = 32 Output: 1 and 30 Input: ar1[]
    7 min read
  • C Program to Calculate Sum of Array Elements
    In this article, we will learn how to find the sum of elements of an array using a C program. The simplest method to calculate the sum of elements in an array is by iterating through the entire array using a loop while adding each element to the accumulated sum. [GFGTABS] C #include <stdio.h>
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences