How to find the second pair of repeated elements in vector with C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm> 

int main ()
{
    std::vector<int> v = { 5, 2, 1, 3, 3, 7, 1, 5, 9, 9, 9, 4, 4 };
    
    std::vector<int>::iterator it;

    it = std::adjacent_find(v.begin(), v.end());

    if (it != v.end())
        it = std::adjacent_find(++it, v.end());

    if (it != v.end())
        std::cout << "The second pair of repeated elements start with: " << *it << '\n';

    return 0;
}
   
   
   
/*
run:
   
The second pair of repeated elements start with: 9
   
*/

 



answered Apr 14, 2020 by avibootz
edited Apr 14, 2020 by avibootz
...