Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
template<typename Value, size_t N>
constexpr std::array<int, N> frequency_map(std::array<Value, N> const& values)
{
std::map<Value, int> freq_map;
for (int i = 0; i < N; ++i)
freq_map[values[i]] += 1; // Count each occurence using a std::map
std::array<int, N> freqs {}; // Reconstruct a std::array from our std::map
for (int i = 0; i < N; ++i)
freqs[i] = freq_map[values[i]];
return freqs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment