TheAlgorithms-C/leetcode/src/1207.c
2019-10-10 11:34:58 -07:00

26 lines
614 B
C

#define MAP_SIZE 2048
int cmpvalue(const void *a, const void *b) {
return *(int *)b - *(int *)a;
}
bool uniqueOccurrences(int* arr, int arrSize){
int *map = calloc(MAP_SIZE, sizeof(int));
int i;
for(i = 0; i < arrSize; i++) {
if (arr[i] < 0)
map[arr[i] + MAP_SIZE/2] += 1;
else
map[arr[i]] += 1;
}
/* number of occurrences is sorted by decreasing order
Ex: 3 2 1 0 0 0 0 */
qsort(map, MAP_SIZE, sizeof(int), cmpvalue);
i = 0;
while(map[i]) {
if(map[i] == map[i+1])
return 0;
i++;
}
return 1;
}