</>EmbeddedInterview

Find duplicate elements in an array.

Easy"Coding: Sorting & Array Problems"
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>

/* values known to be in 0..255 (e.g. bytes, sensor IDs): counting table, O(n) */
void print_duplicates_small(const unsigned char *a, size_t n)
{
    unsigned count[256] = {0};
    for (size_t i = 0; i < n; i++)
        if (++count[a[i]] == 2) printf("%u ", a[i]);   /* print each duplicate once */
    printf("\n");
}

/* general values: sort first (in place), then adjacent duplicates are neighbours */
void print_duplicates_sorted(const int *a, size_t n)
{
    for (size_t i = 1; i < n; i++)
        if (a[i] == a[i - 1] && (i == 1 || a[i] != a[i - 2]))
            printf("%d ", a[i]);
    printf("\n");
}

Points: choose by constraints: small value range → counting table (O(n), O(range) memory); otherwise sort (O(n log n), O(1) extra) or a hash set (O(n) average, O(n) memory); brute force is O(n²). Mention which one fits an MCU (no heap: sort or table).