fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. template<typename T>
  8. typename vector<T>::iterator FindFirstDuplicate( vector<T>& v, typename vector<T>::iterator& dupe )
  9. {
  10. auto current = v.begin();
  11.  
  12. while (
  13. current != v.end() &&
  14. (dupe = std::find( std::next( current ), v.end(), *current )) == v.end()
  15. )
  16. {
  17. ++current;
  18. }
  19.  
  20. return current;
  21. }
  22.  
  23. int main() {
  24.  
  25. vector<int> v = { 1,2,3,4,5,6,5,4,3 };
  26. auto dupe = v.begin();
  27. auto it = FindFirstDuplicate(v,dupe);
  28. cout << "first dupe is " << *it << " at pos " << it - v.cbegin() << endl;
  29. cout << "dupe is " << *dupe << " at pos " << dupe - v.cbegin() << endl;
  30.  
  31. std::rotate( v.begin(), it, v.end() ) ;
  32. return 0;
  33. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
first dupe is 3 at pos 2
dupe is 3 at pos 8