Home > OS >  Print common elements in an array intersection
Print common elements in an array intersection

Time:12-06

Given two arrays - array arr and array art of size n and m respectively. We have to find intersection of arrays

My solution -

#include<iostream>
#include<climits>
using namespace std;

void inputarray(int arr[],int size){
    for(int i=0;i<size;i  ){
        cin>>arr[i];
    }
}

void logic(int arr1[],int size1,int arr2[],int size2){
    for(int i=0;i<size1;i  ){
        int element = arr1[i];
        for(int j=0;j<size2;j  ){
            if(element==arr2[j]){
                cout<<element;
                arr2[j]=INT_MIN;
                break;
                
                
            }
                       
        }
        
    }
}


int main(){
    int arr1[100];
    int arr2[100];
    int size1;
    cin>>size1;
    int size2;
    cin>>size2;

    inputarray(arr,size1);
    inputarray(arr,size2);
    logic(arr1,size1,arr2,size2);



}

But for this abovw solution answer is coming wrong.

Answer Coming is -

6

4

1 2 2 2 3 4

2 2 3 3

2233

Expected Answer is -

6

4

1 2 2 2 3 4

2 2 3 3

223

So please tell where is the problem and how can i solve ?

CodePudding user response:

Errors like this one show how important variable naming is... In the line: if(ele==arr[j]){ you are mistaking arr with art...

  • Related