#include <stdio.h>
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
/**
* Checks if provided arr includes an element starting from {@param headStart}
* position
*
* @returns zero to indicate the element was not found, one to indicate found
*/
int arrContains(int element, int arr[], int arrLen, int headStart) {
for (int i = headStart; i < arrLen; i++) {
if (element == arr[i]) return 1;
}
return 0;
}
void arrFill(int filler, int arr[], int arrLen) {
for (int i = 0; i < arrLen; i++) {
arr[i] = filler;
}
}
int main() {
int arr[] = { 2, 4, 5, 1, 2, 8, 6, 6 };
int size = sizeof(arr) / sizeof(int);
// keep track of individual processed duplicates so that we won't account
// for the same duplicate element twice
int duplicates[size];
int duplicateI = 0;
arrFill(0, duplicates, size); // we can't have random values
// notice that duplicate arr is filled with zeros, but original array
// can also contain duplicates of zeros
int zeroImpact = -1;
for (int i = 0; i < size; i++) {
int it = arr[i];
if (it == 0) { // handle the case of zero separately
zeroImpact++;
continue;
} else if (arrContains(it, duplicates, size, 0)) {
continue; // value is already processed as duplicate
}
if (arrContains(it, arr, size, i + 1)) duplicates[duplicateI++] = it;
}
// if there are no duplicate of zeros, increment by 0; otherwise increment
// by 1 at most
printf("%d duplicates found.", duplicateI + max(0, min(1, zeroImpact)));
}