'How to intersect of more than 2 arrays?
I would like to intersect of more than 2 arrays. Here's incomplete version of my coding :
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
cout << "\n";
string rock[n];
for(int i=0;i<n;i++)
{
cin >> rock[i];
cout << "\n";
}
for(int i=0;i<n;i++)
sort(rock[i].begin(), rock[i].end());
}
So for example if I input
3
asasadfg
aassdw
dasaasf
I want it the output to be like this
ads
No duplication and intersect between more than 2 arrays. What's the best way to do it? Thanks
Solution 1:[1]
- Insert the values in the first array into an output_set.
- Go through the next array and if the element is in the output_set, store it in a new_set.
- swap(new_set, output_set), clear the new_set
- If there is a next array, go to step 2.
- The repeated items are in the output set.
If the items are sorted, the intersection in step 2 can be done more efficiently without using sets.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
