Given an array of elements (including duplicates), I want identify all distinct entries and print their corresponding frequencies into another array. For example: 

```
Input: 3,9,1,2,3,8,13,7,2,9,20,7,4,5,13,1,5,6,2,5,20,13,3,5,6

Output: Sorted unique array: 
        1 2 3 4 5 6 7 8 9 13 20
        Sorted unique array element frequency: 
        2 3 3 1 4 2 2 1 2 3 2

```

I have a code that does this using unordered\_map, but I want to know the most performance efficient way to tackle this using std::vector, std::sort, std::unique etc. Following is the code I presently have that uses std::vector:

```
int main(int argc, char *argv[])
{
    int counter=1;

    std::vector<int> arr1 {3,9,1,2,3,8,13,7,2,9,20,7,4,5,13,1,5,6,2,5,20,13,3,5,6};
    std::vector<int> arr2;

    sort(arr1.begin(), arr1.end());

    printf("Sorted array: \n");
    for (std::vector<int>::iterator it = arr1.begin() ; it != arr1.end(); ++it)
         std::cout << ' ' << *it;
    std::cout << '\n';

    for(int i = 0; i < (int)(arr1.size()-1); i++)
    {
         if (arr1[i] == arr1[i+1]) {
             do {
                  counter++;
                  i++;
             } while(i<(int)arr1.size() && (arr1[i] == arr1[i+1]));
             arr2.push_back(counter);
             counter=1;
         } else
             arr2.push_back(1);
    }

    arr1.erase( unique( arr1.begin(), arr1.end() ), arr1.end() );
    printf("Sorted unique array: \n");
    for (std::vector<int>::iterator it = arr1.begin() ; it != arr1.end(); ++it)
         std::cout << ' ' << *it;
    std::cout << '\n';

    assert(arr1.size() == arr2.size());
    printf("Sorted unique array element frequency: \n");
    for (std::vector<int>::iterator it = arr2.begin() ; it != arr2.end(); ++it)
         std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
 }

```

Presently my code uses 2 passes: pass1- A for loop over entire arr1 to calculate arr2, pass2: std::unique + std::erase to obtain all the distinct entries in arr1.

Is there a more performance efficient way of tackling this, say doing it in 1 pass, without the use of hash\_map/unordered\_map's?