Python guy
Python guy

Reputation: 119

Finding intersection of two arrays

I'm having trouble finding what's wrong with my code - at first I thought I was finished, but then I found some issues. I am creating a function called bagIntersection that takes two bag objects and finds common items in both, and then places them into a new bag:

For example, if bag has items {"b", "b", "c"} and bag2 has items {"b", b", "d", "e"}, the function call:

ArrayBag<std::string> resultBag = bag.bagIntersection(bag2);

should only return string "b" in the resultBag. My function bagIntersection is returning {"b","b"} into the resultBag. So somehow I'm getting multiples of "b".

(code removed) The code was correct.

Upvotes: 0

Views: 337

Answers (1)

doudouremi
doudouremi

Reputation: 186

If you want to remove duplicate you can do like this with a vector container for example :

std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());

and you will have only one "b".

Upvotes: 1

Related Questions