-1
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
     
        int k=0;
    int m=nums1.size();
        int n=nums2.size();
     vector<int> res[m];
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(nums1[i]==nums2[j]){
                    res[k]=nums1[i];
                    k++;
                    break;
                }
            }
        }
        return res;
    }
};

I had to find duplicates in two different arrays and return the same. But I'm getting the error

no viable overloaded '='

from the compiler in the line where I am storing my duplicates in the res[k] vector.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 3
    res is an array of vectors. So if you assign something to res[k], it should be a vector (of ints) – Bas in het Veld Apr 22 '22 at 13:23
  • 4
    `vector res[m];` you probably wanted `vector res(m);` which will be a single vector of m ints instead of a c style array of m empty vectors. – drescherjm Apr 22 '22 at 13:24
  • Side note: You do not use any members of class `Solution`, so step1: the function should rather be *static*, but then no non-static members remain thus step 2: the class should rather be a namespace and static functions get free-standing ones... – Aconcagua Apr 22 '22 at 13:32
  • You have either have a strange understanding of `intersection` or your algorithm doesn't work as you intend: consider `{ 1, 2, 2, 2 }` and `{ 2 }`, result would be `{ 2, 2, 2 }`... – Aconcagua Apr 22 '22 at 13:34
  • I personally would rather sort both vectors then iterate over both similarly to the merge step within merge sort: while current element of one vector is smaller than the current one in the other, skip it; if both are equal add it to the target vector and skip it in *both* source vectors. – Aconcagua Apr 22 '22 at 13:47
  • By the way, that's already implemented: [`std::set_intersection`](https://en.cppreference.com/w/cpp/algorithm/set_intersection) (requiring sorted sources). – Aconcagua Apr 22 '22 at 13:49

1 Answers1

1

In this line you try to allocate an array of std::vectors.

vector<int> res[m];

But m is not constant so it causes a compilation error (C++ does not support VLA - see Why aren't variable-length arrays part of the C++ standard?).

I am not sure what exactly you are trying to do, but I believe you should change it to:

vector<int> res;

Then use push_back to add elements to it:

res.push_back(nums1[i]);    // Instead of: res[k]=nums1[i];

You don't need to track the number of elements in the vector with k and you can eliminate it altogether.

2 more notes:

  1. The method intersect can be a static method (or a free functions) as it doesn't need access to any class members.
  2. Your algorithm implementation compares each element in nums1 to each element in nums2 so you will get duplicates in the output array (not sure what was intended).
wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • Got your point ! Thank you so much – Kashish Bagga Apr 22 '22 at 13:33
  • To avoid multiple re-allocations you might want to add `res.reserve(std::min(n, m))`. However the duplicates problem should to be fixed before. – Aconcagua Apr 22 '22 at 13:36
  • Good point in general @Aconcagua. Although it depends on the case. If m,n are large and but you have very few equal values (between the vectors) you'll reserve a lot more than needed. – wohlstad Apr 22 '22 at 13:38
  • Well, then swapping with a new vector might solve that (`shrink_to_fit` has no guarantees...) as well. One might need to measure, but in most cases/on most systems I'd expect that single re-allocation getting cheaper than multiple ones (just for completeness...). – Aconcagua Apr 22 '22 at 13:44
  • I agree. Measuring each typical use case is anyway a must when dealing with optimizations. – wohlstad Apr 22 '22 at 13:48
  • @KashishBagga consider accepting my answer by clicking the "v" below the score (if it answered your question). – wohlstad Apr 23 '22 at 08:55