Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include<stdio.

h>

void findCommonElements(int array1[], int array2[], int size1, int size2);


//Prototype of function to find common element in arrays

int main(void) {
int array1[8] = {4, 5, 6, 74, 20, 21, 33, 4};
int array2[7] = {5, 55, 5, 4, 5, 74, 99};
int array3[8] = {0};

findCommonElements(array1, array2, 8, 7);

return 0;
}

void findCommonElements(int array1[], int array2[], int size1, int size2) { //


definition of a function
int resultArray[8] = {0};
int same = 0;

for (int i = 0; i < size1; i++) { //main logic


for (int j = 0; j < size2; j++) {
if (array1[i] == array2[j]) {
resultArray[i] = array1[i];
}
}
}

for (int i = 0; i < size1; i++) {


for (int j = 0; j < i; j++) {
if (resultArray[i] == resultArray[j]) {
same++;
}
}
if (same == 0 && resultArray[i] != 0) {
printf("%d ", resultArray[i]);
}
same = 0;
}
}

You might also like