I'll show you how to count occurrences efficiently after sorting. Use bounds: upper_bound(v.begin(), v.end(), x) - lower_bound(v.begin(), v.end(), x) gives the count of x. You can also use adjacent_find to locate where values change after sorting.
This helps when you need to process groups of identical elements rather than just counting them. For multiple queries on the same data, sort once and reuse the sorted order. Each subsequent query becomes instead of linear scans.