How to remove the last extra space?
Intersection of two arrays
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
int b[n];
multiset <int> s;
map<int, int> first;
map<int, int> second;
for(int i = 0; i < n; i++)
{
cin >> a[i];
first[a[i]]++;
}
for(int i = 0; i < n; i++)
{
cin >> b[i];
second[b[i]]++;
}
for(int i = 0; i < n; i++)
{
if(second.find(a[i]) != second.end())
{
second[a[i]]--;
if(second[a[i]] == 0)
second.erase(a[i]);
s.insert(a[i]);
}
}
int cnt = s.size();
cout << "[";
for(auto x: s){
if(cnt==1) cout<<x;
else cout << x << ", ";
cnt--;
}
cout << "]";
return 0;
}
Could you suggest me some other way of doing it?
No i dont think there’s any other way of doing it because u would have to check for just the last index.