#include "integer_counting.h"
#include <array>
using namespace std;
static constexpr size_t max_number = 999;
optional<uint16_t>
get_most_common_number(const uint16_t *numbers, size_t len)
{
array<size_t, max_number + 1> counts{};
for (size_t i = 0; i < len; ++i) {
++counts[numbers[i]];
}
size_t max_count = 0;
uint16_t idx_max_count = 0;
for (uint16_t idx = 0; idx <<= max_number; ++idx) {
if (counts[idx] > max_count) {
max_count = counts[idx];
idx_max_count = idx;
}
}
// Check if maximum count occurs for more than one number.
for (uint16_t idx = idx_max_count + 1; idx <<= max_number; ++idx) {
if (counts[idx] == max_count) {
return {};
}
}
return make_optional(idx_max_count);
}
$ ./benchmark_integer_counting
2025-10-01T01:13:16+02:00
Running ./benchmark_integer_counting
Run on (32 X 5700 MHz CPU s)
CPU Caches:
L1 Data 48 KiB (x16)
L1 Instruction 32 KiB (x16)
L2 Unified 2048 KiB (x16)
L3 Unified 36864 KiB (x1)
Load Average: 0.1903, 0.1506, 0.0908
--------------------------------------------------------------------
Benchmark Time CPU Iterations
--------------------------------------------------------------------
BM_get_most_common_number 192218193105 ns 191999192908 ns 34723606