firstDuplicate of array
Find more appropriate data structures


  This question has been asked for other languages, but I haven't seen it for C++.


Getting timeout usually indicates an inefficient algorithm.
The language doesn't matter, because algorithms are language agnostic.
Some of the posts you linked mention the word "performance",
and they shed light to the problem,
with suggestions that could be applied here too,
even if the language is different.

The first thing to ask when a solution is too slow,
what's the time complexity of the algorithm?


   quadratic: \$O(n^2)\$


That's not so good. Looping over all elements is a linear operation (\$O(n)\$),
and checking if an element is in a vector is also a linear operation.

In other words, the weak spot is using a vector to store the elements already seen.
A vector doesn't provide a fast way to search for random elements.
Other data structures can provide faster than linear performance to lookup elements:
a tree set can do it in logarithmic time (\$O(\log n)\$, making the overall complexity of your algorithm log-linear \$O(n \log n)\$),
or a hash set can do it in constant time (\$O(1)\$, making the overall complexity of your algorithm linear \$O(n)\$).

Coding style

The name newVector is not great, for several reasons:


What's "new" about it?
Including the name of the data structure in the name of the variable usually doesn't give much new information to the reader, and therefore doesn't help understand its purpose.


This vector is used to store elements already seen. I would have called it seen.



The name a is even worse: it tells nothing to the reader about what it is.



The main loop goes over indexes of the input vector.
You could loop over the values of the input,
which would read more natural:

for (int value : a) {
    if (std::find(newVector.begin(), newVector.end(), value) != newVector.end()){
        return value;
    }
    newVector.push_back(value);
}