Home > Backend Development > C++ > C program to implement the intersection operation of two arrays

C program to implement the intersection operation of two arrays

WBOY
Release: 2023-09-22 19:01:07
forward
810 people have browsed it

C program to implement the intersection operation of two arrays

Intersection operation

If array 1 = {1,2,3,4,6}

Array 2 = {1, 2,5,6,7 }

Then, the intersection of array 1 and array 2 is

Array1 ^ array 2 = {1,2,3,4,6} ^ {1,2,5,6,7}
                 = {1,2,6}
Copy after login

A group of common elements is called an intersection.

The logic of intersection is as follows −

k=0;
for(i=0;i<size1;i++){
   for(j=0;j<size2;j++){
      if(a[i]==b[j]){
         intersection[k]=a[i];
         k++;
      }
   }
}
Copy after login

Program

The following is a C program that performs the intersection operation of two arrays−

Demonstration

#include<stdio.h>
int removerepeated(int size,int a[]);
void sort(int size,int a[]);
main(){
   int i,size1,size2,size,j=0,k,intersectionsize;
   printf("Enter size of an array1</p><p>");
   scanf("%d",&size1);
   printf("Enter size of an array2</p><p>");
   scanf("%d",&size2);
   int a[size1],b[size2],uni[size1+size2];
   if(size1<size2){
      intersectionsize=size1;
   }else if(size1>size2){
      intersectionsize=size2;
   }else{
      intersectionsize=size1;
   }
   int intersection[intersectionsize];
   printf("Enter numbers for array 1</p><p>");
   for(i=0;i<size1;i++){
      scanf("%d",&a[i]);
   }
   printf("Enter numbers for array 2</p><p>");
   for(i=0;i<size2;i++){
      scanf("%d",&b[i]);
   }
   //Intersection starts
   k=0;
   for(i=0;i<size1;i++){
      for(j=0;j<size2;j++){
         if(a[i]==b[j]){
            intersection[k]=a[i];
            k++;
         }
      }
   }
   //Sorting
   sort(k,intersection);
   //Removing
   size=removerepeated(k,intersection);
   printf("Array after intersection</p><p>");
   if(size>0){
      for(i=0;i<size;i++){
         printf("%d</p><p>",intersection[i]);
      }
   }else{
      printf("No intersection</p><p>");
   }
}
int removerepeated(int size,int a[]){
   int i,j,k;
   for(i=0;i<size;i++){
      for(j=i+1;j<size;){
         if(a[i]==a[j]){
            for(k=j;k<size;k++){
               a[k]=a[k+1];
            }
            size--;
         }else{
            j++;
         }
      }
   }
   return(size);
}
void sort(int size,int a[]){
   int i,j,temp;
   for(i=0;i<size;i++){
      for(j=i+1;j<size;j++){
         if(a[i]>a[j]){
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
         }
      }
   }
}
Copy after login

Output

When the above program is executed, it produces the following results −

Enter size of an array1
5
Enter size of an array2
2
Enter numbers for array 1
4
5
6
7
8
Enter numbers for array 2
4
1
Array after intersection
4
Copy after login

The above is the detailed content of C program to implement the intersection operation of two arrays. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template