Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active May 12, 2017 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deque-blog/3e71ac622a6434532db3496ae1061057 to your computer and use it in GitHub Desktop.
Save deque-blog/3e71ac622a6434532db3496ae1061057 to your computer and use it in GitHub Desktop.
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