I have this vector
vector <string> data
data = ["this is", "data that", "is in", "this is", "vector", "vector", "vector"]
how do I get a vector (or 2D array) that removes duplicates and instead has the counts for each ith entry?
i.e.
results = [("this is", 2), ("data that", 1), ("is in", 1), ("vector", 3)]
The straightforward solution would be to accumulate the unique values and their counts into a map:
This has linearithmic (n lg n) time complexity, though because it must make a copy of each distinct string value, it may be rather expensive. You could also sort the list in-place, then count the number of each value, which would likely perform better if you have a move-aware implementation of
std::string.