Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Couting unique elements, with a peculiar definition of equality.
#include "elements.h"
#include <stddef.h>
#include <stdbool.h>
static int compare(const element *a, const element *b)
{
if (a->e2 == b->e2)
return 0;
else if (a->e1 != b->e1)
return b->e1 - a->e1;
return b->e2 - a->e2;
}
size_t count_unique(const element *s)
{
size_t c = 0;
bool found;
for (size_t i = 0; s[i].s != NULL; ++i) {
found = false;
for (size_t j = 0; j < i; ++j) {
if (compare(&s[i], &s[j]) == 0) {
found = true;
break;
}
}
if (!found)
c++;
}
return c;
}
#ifndef ELEMENTS_H
#define ELEMENTS_H
#include <stddef.h>
typedef struct
{
const char *s;
int e1;
int e2;
} element;
size_t count_unique(const element *s);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.