I finally figured it out. I created a search function to make sure I'm not copying any duplicated element into array 3. It searches through the elements in array 3 and inserts elements the elements if they don't exist.
Thank you for helping me figure it out!
Code:
#include <stdio.h>
#include <stdlib.h>
void merge_array(int arr1[], int num1, int arr2[], int num2);
int main(void){
int first_arr[100];
int second_arr[100];
int num1, num2;
int i;
printf("Enter the length of array 1: ");
scanf("%d", &num1);
printf("Enter the elements of the array: ");
for(i = 0; i < num1; i++){
// scan the first array of elements
scanf("%d", &first_arr[i]);
}
printf("Enter the length of the array 2: ");
scanf("%d", &num2);
printf("Enter the elements of the array: ");
for(i = 0; i < num2; i++){
// scan the second array of elements
scanf("%d", &second_arr[i]);
}
printf("Output array: ");
merge_array(first_arr, num1, second_arr, num2);
return 0;
}
// The search function is used to make sure that we dont
// copy any duplicate elements into arr3
int search(int arr3[], int num1, int num2){
int i;
// loop through the elements in the arr3
// make sure to avoid copying any
// duplicate elements into arr3
for(i = 0; i < num1; i++){
if(arr3[i] == num2){
return i;
}
}
return -1;
}
// Function used to merge the two arrays into a new array called
// arr3 and displays each element, without duplicates
void merge_array(int arr1[], int num1, int arr2[], int num2){
// contcatinating the elements from arr1 and arr2
int arr3[num1 + num2];
int i, k = 0;
// loop through every element in the array
// and remove any duplicate elements that
// are found in both arrays.
//output the array
for(i = 0; i < num1; i++){
// copy the elements from arr1 into arr2
arr3[k] = arr1[i];
k++;
}
for(i = 0; i < num2; i++){
// search the elements in arr3
// if the elements are not present
// then insert the element into arr3
if(search(arr3, num1, arr2[i]) == -1){
// copy the elements from arr3 into arr3
arr3[k] = arr2[i];
k++;
}
}
//output array
for(i = 0; i < k; i++){
printf("%d ", arr3[i]);
}
}