Common elements

Write a function

        int common_elements(const vector<int>& X, const vector<int>& Y);

that given two vectors @X@ and @Y@ in increasing order, returns the
number of common elements in the two vectors, that is, the number of
integer numbers a such that a = X[i] = Y[j] for any i and j.

Precondition

The two vectors are in strictly increasing order.

Samples

@common_elements@([3, 5, 7, 8], [2, 3, 7, 9, 10]) = 2.
@common_elements@([1, 2, 3, 4, 5], [3, 4, 5, 6, 7, 8]) = 3.
@common_elements@([1, 2, 3, 4, 5], [8, 9]) = 0.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T10:37:53.258Z

© Jutge.org, 2006–2026.
https://jutge.org
