fork(8) download
  1. #include <vector>
  2. #include <map>
  3. #include <iostream>
  4.  
  5. std::vector<std::size_t> findDuplicateIndices(std::vector<int> const & v)
  6. {
  7. std::vector<std::size_t> indices;
  8. std::map<int, std::pair<int, std::size_t>> counts;
  9.  
  10. for (std::size_t i = 0 ; i < v.size() ; ++i)
  11. {
  12. std::size_t amount = ++counts[v[i]].first;
  13. /**/ if (amount == 1)
  14. {
  15. counts[v[i]].second = i;
  16. continue;
  17. }
  18. else if (amount == 2)
  19. indices.push_back(counts[v[i]].second);
  20.  
  21. indices.push_back(i);
  22. }
  23. return indices;
  24. }
  25.  
  26. int main()
  27. {
  28. auto duplicateIndices = findDuplicateIndices({1,1,2,3,4,1,3});
  29. for (auto i : duplicateIndices) std::cout << i << ',';
  30. std::cout << std::endl;
  31. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0,1,5,3,6,