C Program To Merge Two Arrays

// Merging two arrays with duplicates

#include <stdio.h>
int main()
{
        int  i,j ;
        int arr_A[3];
        int arr_B[3];
        int arr_C[6];
   
        printf("Enter elements of first array, number of elements to be input is 3: ");
       for(i=0;i<3;i++)
       {
           scanf("%d", &arr_A[i]);
       }
   
   printf("Enter elements of second array, number of elements to be input is 3: ");
       for(i=0;i<3;i++)
       {
           scanf("%d", &arr_B[i]);
       }
   
       printf("Combining two arrays: ");
   
       for (i=0;i<3;i++)
       {
            arr_C[j]=arr_A[i];
            j++;
        }

        for(i=0;i<3;i++)
        {
            arr_C[j]=arr_B[i];
            j++;
        }

   printf("elements in third array are: ");

         for(i=0;i<6;i++)
         {
              printf("%d  ", arr_C[i]);
          }
    return 0;
}
Output:
Enter elements of first array, number of elements to be input is 3: 
1

2

3

Enter elements of second array, number of elements to be input is 3: 
4

5

6

Combining two arrays: elements in third array are: 1  2  3  4  5  6  

Code Analysis

Above code uses a for loop to combine two arrays. First & Second array size is of 3 integer elements. Third array which combines first & second arrays must have size equal to the combined size of first and second array and its data type must be same as that of first and second array.

// Merging two without duplicate values

#include <stdio.h>

int main()
{
   int  i,k, flag = 0, fd = 0;
   int j = 0;
   int arr_A[3];
   int arr_B[3];
   int arr_C[6];
   
   printf("Enter elements of first array, number of elements to be input is 3: ");
   for(i=0;i<3;i++)
   {
       scanf("%d", &arr_A[i]);
   }
   
   printf("Enter elements of second array, number of elements to be input is 3: ");
   for(i=0;i<3;i++)
   {
       scanf("%d", &arr_B[i]);
   }
   
   printf("Combining two arrays: ");
   
   for (i=0; i<3; i++)
   {
       for(k=0;k<3;k++)
       {
           if(arr_A[i]==arr_B[k])
           {
               arr_B[k]=000;
           }
       }
       
   }
   for(i=0,j=0;i<3;i++,j++)
   {
       arr_C[j] = arr_A[i];
   }
   for(i=0;i<3;i++)
   {
       if(arr_B[i]==000)
       {
           fd++;
       }
       else
       {
           arr_C[j]=arr_B[i];
           j++;
       }
   }
   for(j=0;j<6-fd;j++)
   {
       printf("%d  ", arr_C[j]);
   }
   return 0;
}
Output:
Case I:

When both the arrays are same:

Enter elements of first array, number of elements to be input is 3: 
1

2

3

Enter elements of second array, number of elements to be input is 3: 
1

2

3

Combining two arrays: 1  2  3

Case II:

When only one element is common.

Enter elements of first array, number of elements to be input is 3: 
1

2

3

Enter elements of second array, number of elements to be input is 3: 
4

2

5

Combining two arrays: 1  2  3  4  5  

Case III: When two elements are common.

Enter elements of first array, number of elements to be input is 3: 
1

2

3

Enter elements of second array, number of elements to be input is 3: 
4

3

2

Combining two arrays: 1  2  3  4  

Case III: When no elements are common.

Enter elements of first array, number of elements to be input is 3: 
1

2

3

Enter elements of second array, number of elements to be input is 3: 
4

5

6

Combining two arrays: 1  2  3  4  5  6 
Code Analysis

This code merges two arrays and puts it into a third array. In merging two arrays two case arises:

Case - I: when both the arrays are the same, that is both arrays have exactly the same elements or we can say that when both arrays are copies of each other.

Case - II: When array contains duplicate values - one or more elements in common.

The output block gives output of these cases.

Time complexity of the above code is O(n) where n is the number of lines in the code. Time complexity ‘for loop’ and ‘while loop’ is almost same. This code uses array data structure and to access, insert, delete an item of an array it takes O(1) time, O(1) is possible if the index of the array element is known in advance. But if array index is not known then insertion, deletion, searching may take O(n) times. Thus to reduce time complexity and improve code efficiency direct access techniques must be used instead of sequential access techniques.  

// Merging two character arrays

#include <stdio.h>
int main( ) 
{
       char arr_A[100] = "This is ", arr_B[] = "digiRake";
       int l_en, q;

       printf("\b First Character Array is: \t");
       puts(arr_A);

       printf("\n");

       printf("\b Second Character Array is: \t");
       puts(arr_B);

       printf("\n");
 
       l_en = 0;
  
      while (arr_A[l_en] != '\0') 
      {
            ++l_en;
      }

      for (q = 0; arr_B[q] != '\0'; ++q, ++l_en) 
     {
           arr_A[l_en] = arr_B[q];
     }

     arr_A[l_en] = '\0';

     printf("After merging : ");
     puts(arr_A);

     return 0;
}
Output:

First Character Array is: 	   This is 

Second Character Array is:    digiRake

After merging : This is digiRake
Code Analysis:
This code merges two character arrays. To print character array puts function is used. puts( ) is used to write a line or string on the console window. It writes sting on the output window and returns an integer value.

puts( ) function is different from printf( ). printf( ) function can print both string and variable on the output window while puts( ) can only print string on the screen.    

Strings in the C language are terminated by the null character ‘\0’. Above code uses a while loop. Terminating condition of the while loop is to check for ‘\0’.

In this code a pre-increment operator is used. Increment operators are used to increase the value by 1. In case of pre-increment variable value is increased by 1 and than it is assigned/used.

In case of post increment, the variable is first assigned/used than it is incremented by 1.

For example, if the value of variable i is 5, then ++i will first increment value and then other operations will be performed on it. While i + + is post increment so first other operations will be performed on it then it will increment value on it.