← Back to Templates
Question 750
Count Equal Elements in an Array
Count Equal Elements in an Array
Write a function template countMatches(const T values[], int size, T target) that returns, as an int, how many of the first size elements are equal to target.
Because the array elements and target share the type parameter T, both arguments must agree: with an array of std::string, the target has to be a std::string too, not a bare "cow" literal. The tests print one count per line for: an int array {3, 7, 3, 3, 9} with target 3; a char array {'a', 'b', 'a'} with target 'a'; a std::string array {"cat", "dog"} with target "cow"; and the int array again with a size of 0.
Do NOT write a main function.
Expected Output:
3 2 0 0
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Templates
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.