It may depend on what you mean by βbetter,β but of course there are ways that are simpler and other ways that are probably faster.
The easiest way is to insert elements into std::set or std::unordered_set . When you inserted all of them, the size of the set will be the number of unique elements.
Probably a faster method is to use std::sort and std::unique to find unique elements in place instead of copying them. This is pretty much what std::unique_copy will usually do internally, but doing it in place saves a fair amount of distribution and copying.
std::vector<Items> v; // populate v with data std::sort(v.begin(), v.end()); int uniqueCount = std::unique(v.begin(), v.end()) - v.begin();
source share