I have a vector with different values and some of them may appear twice. (Only twice.)
How can I find the FIRST duplicate item?
Like: [a][b][b][a]
Then I’d need ‘b’.
(Sorry for the newbie question.)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re looking for adjacent duplicates, you can simply use
std::adjacent_find.If the duplicates aren’t necessarily adjacent, then you could first(See @aix’s comment below)std::sortthe vector, and then usestd::adjacent_findon the result.Alternatively, you could push each element into a
std::set, and look for collisions as you’re doing it.