codescracker
c++

C++ Program to Find and Print Common Elements between Two Arrays



« C++ Tutorial C++ Example »

This article deals with some programs in C++ that are used to find and print all the common elements present between two given arrays by user at run-time.

For example, if the two given arrays are:

arr1[10] = 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
arr2[10] = 10, 34, 10, 67, 54, 90, 84, 39, 30, 54

then common elements between these two arrays are 10, 90, 30. Because, these three elements are available in both the arrays.

Find Common Elements between Two Arrays

The question is, write a program in C++ that receives any two arrays from user at run-time, to find and print all the common elements between two given arrays. Here is its answer:

#include<iostream>

using namespace std;

int main()
{
   int arr1[10], arr2[10], arr3[10], i, j, k=0, x, tot;
   cout<<"Enter 10 Elements for First Array: ";
   for(i=0; i<10; i++)
      cin>>arr1[i];
   cout<<"Enter 10 Elements for Second Array: ";
   for(i=0; i<10; i++)
      cin>>arr2[i];

   // Finding and storing common elements
   for(i=0; i<10; i++)
   {
      for(j=0; j<10; j++)
      {
         if(arr1[i]==arr2[j])
         {
            tot = 0;
            for(x=0; x<k; x++)
            {
               if(arr1[i]==arr3[x])
                  tot++;
            }
            if(tot==0)
            {
               arr3[k] = arr1[i];
               k++;
            }
         }
      }
   }

   // Printing common elements
   cout<<"\nCommon Elements are:\n";
   for(i=0; i<k; i++)
      cout<<arr3[i]<<" ";

   cout<<endl;
   return 0;
}

The snapshot given below shows the initial output produced by above C++ program on finding and printing all the common elements between two given arrays by user at run-time:

c++ program find common elements between two arrays

Now supply the input say 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 as ten elements for first array and 11, 12, 10, 13, 10, 14, 2, 15, 6, 6 as ten elements for second array. Here is the sample run based on exactly these inputs:

print common elements between two arrays c++

The above program is created in a way that:

Find Common Elements between Two Arrays of any Size

This is the modified version of previous program. This program allows user to define the size of both the array. Also this program prints the message according to the output. That is, if no common elements found between given two arrays, then the program prints the message says Common Element not Found!. This provides better user-experience.

#include<iostream>

using namespace std;

int main()
{
   int a[100], b[100], c[100], i, j, k=0;
   int a_size, b_size, x, cnt=0;
   cout<<"Enter the Size of First Array (max.100): ";
   cin>>a_size;
   cout<<"Enter "<<a_size<<" Elements for First Array: ";
   for(i=0; i<a_size; i++)
      cin>>a[i];
   cout<<"Enter the Size of Second Array (max.100): ";
   cin>>b_size;
   cout<<"Enter "<<b_size<<" Elements for Second Array: ";
   for(i=0; i<b_size; i++)
      cin>>b[i];
   for(i=0; i<a_size; i++)
   {
      for(j=0; j<b_size; j++)
      {
         if(a[i]==b[j])
         {
            cnt = 0;
            for(x=0; x<k; x++)
            {
               if(a[i]==c[x])
                  cnt++;
            }
            if(cnt==0)
            {
               c[k] = a[i];
               k++;
            }
         }
      }
   }
   if(k==0)
      cout<<"\nCommon Element not Found!";
   else if(k==1)
   {
      cout<<"\nHere is only 1 Common Element:\n";
      cout<<c[0];
   }
   else
   {
      cout<<"\nHere are all "<<k<<" Common elements:\n";
      for(i=0; i<k; i++)
         cout<<c[i]<<" ";
   }
   cout<<endl;
   return 0;
}

Here is its sample run with user inputs, 3 as size of first array, 1, 2, 3 as three elements for first array. And 4 as size of second array, 4, 5, 6, 7 as four elements for second array:

c++ print all common elements between two arrays

Here is another sample run with user inputs, 5 and 1, 2, 3, 4, 5 as size and elements for first array, whereas 7 and 1, 6, 7, 8, 9, 10, 11 as size and elements for second array:

print all common elements between two arrays c++

C++ Online Test


« C++ Tutorial C++ Example »



© Copyright 2021. All Rights Reserved.

CodesCracker