Intersection of Arrays in C | Set Operations

 The intersection of two arrays A and B is the set of all elements which belong to both A and B.

This article deals with the implementation of intersection operation on unsorted arrays. (Click here if you wish to view the C++ version).


Set Operation on Arrays
Intersection Of Arrays


The following header file is used -

  • stdio.h - For standard input or output operations.


The following user-defined functions are created -

  • displayed - This function is used to display the array. This function receives an array pointer and the size of the array as parameters and does not return any value. 
  • intersection - This function is used to perform intersection operations on arrays. This function receives 3 array pointers of which union operation is performed on two arrays and the result is stored in the 3rd array and 2 integer values which are the lengths of the arrays on which the union operation is performed. This function returns the length of the resultant array.


C Code - 

#include<stdio.h>

void displayed(int a[], int n)
{
    for(int i=0;i<n;i++)
    {
        if(i==n-1)
        {
            printf("%d.",a[i]);
        }
        else{
           printf("%d, ",a[i]);
        }
    }
}

int intersection(int a[], int n, int b[], int m, int res[])
{
    int count=0,k=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i]==b[j])
            {
                res[k++]=a[i];
                count++;
            }
        }
    }
   
    return (count);
}

int main()
{
    int n,m,l;
    int ar1[]={1,4,9,5,16,32};
    int ar2[]={1,2,3,4,5,6,7,8,9,10};
    n=sizeof(ar1)/sizeof(ar1[0]);
    printf("\nArray1: ");
    displayed(ar1,n);
    m=sizeof(ar2)/sizeof(ar2[0]);
    printf("\nArray2: ");
    displayed(ar2,m);
    //size of smaller array
    int un[n];
    int size=intersection(ar1,n,ar2,m,un);
    //Here size is the length of intersection
    printf("\nThe Intersection: ");
    displayed(un,size);

    return 0;
}


Subject - 

In the above example, I have implemented a C program to perform intersection operation on unsorted arrays.


Basic features - 

  • This program performs an intersection operation on any two unsorted arrays.
  • The arrays on which the intersection operation is performed are hardcoded into the program.
  • Implements functional programming paradigm.


Working - 

  1. In this program, the two unsorted arrays(ar1,ar2) [on which the intersection operation is performed] are hardcoded into the program.
  2. The lengths of the two arrays(ar1,ar2) are calculated and stored(n,m) and the elements of the arrays are printed separately via the displayed function.
  3. In order to perform the intersection operation, we create another array(un) to store the result. Since we don't know the result yet, we assume the worst case (i.e all the elements of one array are present in another array) and keep its size as the length of the smallest array.
  4. We pass the 2 arrays(ar1,ar2) upon which the union operation is to be performed along with their sizes and the 3rd array created to store their result into the intersection function.
  5. The intersection function compares the elements of both the arrays and only if the elements match, the common element is inserted into the resultant array(un)
  6. We use the count variable to count the length of the resultant array, which increments its value each time an element is inserted into the resultant array. This value is returned. (This is done to only display the elements which are the result of intersection operation and not garbage value. See point 3 for clear understanding).
  7. Since the arrays are passed by address, the changes made to the formal parameters in the function will be reflected in the actual parameters i.e. will be reflected on the resultant array un.
  8. The resultant array and its length are passed on to the displayed function to display the result. 


Output - 

set operations on arrays
Sample output



Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.

Post a Comment

0 Comments