data-structures · beginner · ~15 min

Count distinct ints in a sorted array

Single-pass linear scan with state.

Challenge

Implement size_t count_distinct_sorted(const int *a, size_t n) for a sorted array.

Starter code

#include <stddef.h>
size_t count_distinct_sorted(const int *a, size_t n) { /* TODO */ return 0; }

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.