How to find common elements between two vectors and print the sum of the common elements? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to find common elements between two vectors and print the sum of the common elements?

27th Aug 2018, 11:23 AM
Tincture
2 Answers
+ 8
A for each loop iterating through each element of vector A, which contains std::find to get matching elements in vector B, and then adding all matching elements to a variable sum.
27th Aug 2018, 11:28 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Ayshi , in addition to what Hatsy Rei has said, just look for count of elemnt in both vactor before you add elemnt to summation variable.. once you do find on other vector , it doesn't take care of repeated element.. for example, in case of vector a, 5 is present at third and sixth element where as in vector b, 5 is present at 4 position... for both entry of 5 in vector a, you should not add 5 to summation variable.
27th Aug 2018, 12:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar