Open In App

C Program to Find Common Array Elements

Improve
Improve
Like Article
Like
Save
Share
Report

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, 7, 8, 9, 10 };
    int c[10], i, j, k = 0, x, count;
    for (i = 0; i < 7; i++) {
        for (j = 0; j < 7; j++) {
  
            // checking if element in
            // array 1 is equal to
            // the element in array 2
            if (a[i] == b[j]) {
                count = 0;
                for (x = 0; x < k; x++) {
                    if (a[i] == c[x])
                        count++;
                }
  
                // storing common elements in c array
                if (count == 0) {
                    c[k] = a[i];
                    // incrementing the k value to store the
                    // size of array 3
                    k++;
                }
            }
        }
    }
    printf("Common array elements is \n");
    for (i = 0; i < k; i++)
        
        // printing the result
        printf("%d ", c[i]);
    return 0;
}


Output

common array elements is 
5 6 


Last Updated : 03 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads