Removing duplicate elements when merging two arrays
I'm having issues trying to figure out how to remove duplicate elements when merging two arrays. The problem I am working on is as follows:
Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array.
Example input/output #1:
Enter the length of array 1: 5
Enter the elements of the array: 8 4 1 3 9
Enter the length of array 2: 3
Enter the elements of the array: 5 4 8
Output array: 8 4 1 3 9 5
Example input/output #2:
Enter the length of array 1: 4
Enter the elements of the array: 3 4 1 7
Enter the length of array 2: 2
Enter the elements of the array: 6 9
Output array: 3 4 1 7 6 9
Here's what I have so far:
Code:
#include <stdio.h>
void merge_array(int arr1[], int num1, int arr2[], int num2);
int main(void){
int first_arr[30];
int second_arr[30];
int num1, num2;
printf("Enter the length of array 1: ");
scanf("%d", &num1);
printf("Enter the elements of the array: ");
for(int 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(int 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;
}
void merge_array(int arr1[], int num1, int arr2[], int num2){
int arr3[60];
int i, j, k = 0;
for(i = 0; i < num1; i++){
//copy all the elements from the first array into third array
arr3[k++] = arr1[i];
}
for(j = 0; j < num2; j++){
// copy all the elements from the second array into third array
arr3[k++] = arr2[j];
}
for(i = 0; i < (num1 + num2); i++){
printf("%d ", arr3[i]);
}
}
Any help is greatly appreciated. Thanks!