logo CodeStepByStep logo

numInCommon

Language/Type: C++ Set Vector collections
Related Links:
Author: Marty Stepp (on 2016/06/29)

Write a function numInCommon that accepts as parameters two references to vectors of integers, and returns a count of the number of unique integers that appear in both vectors. Use one or more Sets as auxiliary storage to help you solve this problem.

For example, if two vectors v1 and v2 had the following values:

v1 = {3, 7, 3, -1, 2, 3, 7, 2, 15, 15}
v2 = {-5, 15, 2, -1, 7, 15, 36}

Your method would return 4 because the elements -1, 2, 7, and 15 occur in both vectors. If the two vectors do not have any elements in common, or if either vector is empty, return 0. Do not modify either vector that is passed in.

Type your solution here:


This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.