Encipher
Encipher

Reputation: 2916

c++ identical element removal from two arrays which do not contain the number itself in

Problem Statement: There are two arrays. Some elements are common on those arrays. Like arr1={5,2,6}; and arr2= {7,9,5,2,3}. Therefore, 5,2 are common elements because they exist on both arrays. As per the question I need to eliminate common elements from arrays. So the resulting arrays contain arr1={5} and arr2={7,9,3}.

My approach is first to merge both arrays within a third array and then eliminate duplicate elements using set operation. But in this approach, the element that appears 2nd time is deleted but the element itself in the list. Also, I get one list instead of two separate lists.

My code:

#include <iostream>
#include <string>
#include<unordered_set>

int main()
{
    std::unordered_set<int> remove_duplicate;
    int arr[3]={5,2,6};
    int arr1[5]={7,9,5,2,3};
    int m= sizeof(arr)/sizeof(arr[0]);
    int n= sizeof(arr1)/sizeof(arr1[0]);
    int mergearr[m+n];
    std::copy(arr,arr+m,mergearr);
    std::copy(arr1,arr1+n,mergearr+m);
  for(int i=0;i<(m+n);i++){
  std::cout<<mergearr[i]<< ' ';
  }
  std::cout<< '\n';


 for(int j=0;j<(m+n);j++){
  remove_duplicate.insert(mergearr[j]);
 }
 for(auto it=remove_duplicate.begin();it!=remove_duplicate.end();++it){

  std::cout<<' '<<*it;
  }
  return 0;
}

Output:

5 2 6 7 9 5 2 3 
  3 9 7 6 2 5 

How can I solve this problem? Thank you in advance.

Upvotes: 0

Views: 2136

Answers (2)

PaulMcKenzie
PaulMcKenzie

Reputation: 35454

First, arrays are fixed in size, so erasing an element from an array is not possible, unless you define what is meant by "erasing from an array". Thus the more appropriate container to use is std::vector<int>.

Given that, if you are allowed to sort both sequences, you can use std::set_intersection to first find the common elements. Then use std::remove_if on both vectors using the generated set of common elements.

Here is an example:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
#include <unordered_set>

int main()
{
    std::vector<int> arr = {5,2,6};
    std::vector<int> arr1 = {7,9,5,2,3};

    // First, sort both containers
    std::sort(arr.begin(), arr.end());
    std::sort(arr1.begin(), arr1.end());

    // Next, call std::set_intersection to collect the common elements into 
    // the unordered_set
    std::unordered_set<int> common;
    std::set_intersection(arr.begin(), arr.end(), arr1.begin(), arr1.end(), 
                          std::inserter(common, common.begin()));

    // Now erase the items in each vector that matches the items in the set
    arr.erase(std::remove_if(arr.begin(), arr.end(), 
                             [&](int n) { return common.count(n); }), arr.end());
    arr1.erase(std::remove_if(arr1.begin(), arr1.end(), 
                             [&](int n) { return common.count(n); }), arr1.end());

    // Output results
    for (auto i : arr)
       std::cout << i << " ";
    std::cout << "\n";
    for (auto i : arr1)
       std::cout << i << " ";
}

Output:

6 
3 7 9 

Upvotes: 2

Nobody
Nobody

Reputation: 1

As PaulMcKenzie's comment says, you are looking to delete all items in the set intersection of the lists. Either use std::set_intersection or iterate through elements in the second array and check if they are in the first (using a set).

Upvotes: 0

Related Questions