mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
26 lines
614 B
C
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;
|
|
}
|