C Program to Find Common Array Elements Between Two Arrays
Last Updated :
07 Aug, 2022
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 force
- Using Merge Sort and then Traversing
Input:
array1[] = {8, 2, 3, 4, 5, 6, 7, 1}
array2[] = {4, 5, 7, 11, 6, 1}
Output:
Common elements are: 4 5 6 7 1
Approach 1:
This is a brute force approach, simply traverse in the first array, for every element of the first array traverse in the second array to find whether it exists there or not, if true then check it in the result array (to avoid repetition), after that if we found that this element is not present in result array then print it and store it in the result array.
C
// C Program to demonstrate scrunity of
// 2 Common Array Elements Using Brute force
#include <stdio.h>
int main()
{
int array1[] = { 8, 2, 3, 4, 5, 6, 7, 1 };
int array2[] = { 4, 5, 7, 11, 6, 1 };
int i, j, flag, x, k = 0;
int result[100];
printf("Common elements are: ");
// To traverse in array1.
for (i = 0; i < sizeof(array1) / 4; i++) {
// To traverse in array2.
for (j = 0; j < sizeof(array2) / 4; j++) {
// To match elements of array1 with elements of
// array2.
if (array1[i] == array2[j]) {
flag = 0;
// To traverse in result array.
for (x = 0; x < k; x++) {
// Check whether found element is
// already present in result array or
// not.
if (result[x] == array1[i]) {
flag++;
}
}
// If we found a new element which is common
// in both arrays then store it in result
// array and print it.
if (flag == 0) {
result[k] = array1[i];
printf("%d ", result[k]);
k++;
}
}
}
}
}
OutputCommon elements are: 4 5 6 7 1
Time Complexity: O(m*n*k)
Auxiliary Space: O(max(m,n)), as in worst case all the elements could be distinct and common.
Approach 2:
This logic can be applied to the sorted array, if the sorted array is not given then sort it using merge sort as its time complexity is less and then apply this approach.
- Traverse in an array using two variables, i for array1 and j for array2.
- Check if the element of array1 is less than the element of array2 then i++.
- Check if the element of array2 is less than the element of array1 then j++.
- Check if the element of array1 is equal to the element of array2 then check whether it is printed before or not if not then print it and store it in the result array.
C++
// C Program to demonstrate scrunity of
// 2 Common Array Elements Using Merge
// Sort and then Traversing
#include <stdio.h>
int main()
{
int array1[] = { 1, 2, 2, 3, 5, 6, 7, 8, 18, 29, 37 };
int array2[] = { 2, 2, 4, 5, 7, 9, 10, 18 };
int i = 0, j = 0, flag, x, k = 0;
int result[100];
// Calculate size of arrays
int array1_size = sizeof(array1) / sizeof(array1[0]);
int array2_size = sizeof(array2) / sizeof(array2[0]);
printf("Common elements are: ");
// Step 1
while (i < array1_size && j < array2_size) {
// Step 2
if (array1[i] < array2[j]) {
i++;
}
// Step 3
else if (array1[i] > array2[j]) {
j++;
}
// Step 4
else {
flag = 0;
// To traverse in result array.
for (x = 0; x < k; x++) {
// Check whether found element is already
// present in result array or not.
if (result[x] == array1[i]) {
flag++;
}
}
// If we found a new element which is common in
// both arrays then store it in result array and
// print it.
if (flag == 0) {
printf("%d ", array1[i]);
result[k] = array1[i];
k++;
}
i++;
j++;
}
}
}
OutputCommon elements are: 2 5 7 18
Time Complexity: O((m+n)*k)
Auxiliary Space: O(max(m,n)), as in worst case all the elements could be distinct and common.
Similar Reads
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 program to find the common array elements #include <stdio.h> int main() { int a[6] = { 1, 2, 3, 4, 5, 6 }; int b[6] = { 5, 6,
1 min read
C Program To Merge Two Arrays Merging two arrays means combining/concatenating the elements of both arrays into a single array.ExampleInput: 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]Out
3 min read
C Program to Check Whether Two Matrices Are Equal or Not Here, we will see how to check whether two matrices are equal or not using a C Program Input: First Matrix: 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 Second matrix: 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 Output: Matrices are equal.Approach: For any two matrices to be equal, the number of rows
2 min read
C Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists hav
6 min read
C Program to Find a pair with the given difference Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples: Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78 Output: Pair Found: (2, 80) Input: arr[] = {90, 70, 20, 80, 50}, n = 45 Output: No Such Pair Recommended: Please solve it on "PRA
3 min read