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

1 Answer

0 votes
#include <iostream>
#include <list>
#include <algorithm> 
 
int main ()
{
    std::list<int> l = { 3, 2, 1, 5, 5, 7, 1, 7, 7, 7, 8, 8 };
     
    std::list<int>::iterator it;
 
    it = std::adjacent_find(l.begin(), l.end());
 
     if (it != l.end())
        it = std::adjacent_find(++it, l.end());
 
    if (it != l.end())
        std::cout << "The second pair of repeated elements start with: " << *it << '\n';
 
 
    return 0;
}
    

    
/*
run:
    
The second pair of repeated elements start with: 7
    
*/

 



answered Apr 15, 2020 by avibootz
...