How to find all duplicate elements within a string vector, and output their indexes when found

I'm currently modifying an existing function that was originally built for searching elements for an int vector and I want to make it work for a string vector instead however I'm struggling to wrap my mind as to why converting all int into string doesn't work for the function below.

size_t amount doesn't = ++counts[v[I]].first; and I'm confused as to why, even reading the error message is confusing to me and would like an explanation.


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


Output:
 
0,1,5,3,6,


How would you modify the following code above to allow string elements as well? (order of output can be important? as in only the first 2 outputs are important, you can replace '1' with 'test', '2' with 'test2', etc.)
Last edited on
.
Last edited on
The output should be the indexes of the elements found in the vector string, so in your example it would output something like:

 
0,8,1,4,3,6,7,9


because

0 and 8 are the indexes of where test1 elements is found

1 and 4 are the indexes of where test2 elements is found

3 and 6 are the indexes of where test3 elements is found

and so on.
Last edited on
The output should be blah blah

@OP
There's enough information in what I've given you to sort that out yourself. Time to take ownership :)

@OP You are beautiful when you get angry. Sorry I wasn't able to help so I removed it. Good luck for your future :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;

template <typename T>
vector<int> findDuplicateIndices( vector<T> const &V )
{
   map<T,vector<int>> M;
   for ( int i = 0; i < V.size(); i++ ) M[V[i]].push_back( i );

   vector<int> indices;
   for ( auto &pr : M ) if ( pr.second.size() > 1 ) indices.insert( indices.end(), pr.second.begin(), pr.second.end() );
   return indices;
}


template <typename T>
ostream & operator << ( ostream &out, vector<T> V )
{
   for ( T e : V ) out << e << ' ';
   return out;
}

 
int main()
{
   cout << findDuplicateIndices<int>( { 1, 1, 2, 3, 4, 1, 3 } ) << '\n';
   cout << findDuplicateIndices<string>( { "Putin", "is", "a", "war", "criminal", "Putin", "is", "insane" } ) << '\n';
}


0 1 5 3 6 
0 5 1 6 

Last edited on
Topic archived. No new replies allowed.