aboutsummaryrefslogtreecommitdiff
path: root/unique_num_occurrences.c
blob: 82449d9dda683d643167eacbb3a948265f779a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 1207. Unique Number of Occurrences
#include "leetcode.h"

/*
 * given an array of integers 'arr', return 'true' if the number of occurences
 * of each value in the array is unique or 'false' otherwise
 */

int cmp(const void *a, const void *b) {
  return *(const int *)b - *(const int *)a;
}

int find_size(int *arr, int arr_size, int *min) {
  int max = arr[0];
  for (int i = 1; i < arr_size; i++) {
    max = arr[i] > max ? arr[i] : max;
    *min = arr[i] < *min ? arr[i] : *min;
  }
  return *min < 0 ? max + abs(*min) + 1 : max + 1;
}

bool uniqueOccurences(int *arr, int arr_size) {
  int shift = arr[0], size = find_size(arr, arr_size, &shift);
  shift = (shift > 0) ? 0 : abs(shift);
  int *hash = (int *)calloc(size, sizeof(int));
  for (int i = 0; i < arr_size; i++)
    hash[arr[i] + shift]++;
  qsort(hash, size, sizeof(int), cmp);
  for (int i = 0; i + 1 < size; i++) {
    if (!hash[i + 1])
      break;
    if (hash[i] == hash[i + 1]) {
      free(hash);
      return false;
    }
  }
  free(hash);
  return true;
}

int main() {
  int a1[] = {1, 2, 2, 1, 1, 3}, a2[] = {1, 2},
      a3[] = {-3, 0, 1, -3, 1, 1, 1, -3, 10, 0};
  printf("%d\n", uniqueOccurences(a1, ARRAY_SIZE(a1))); // expect: 1
  printf("%d\n", uniqueOccurences(a2, ARRAY_SIZE(a2))); // expect: 0
  printf("%d\n", uniqueOccurences(a3, ARRAY_SIZE(a3))); // expect: 1
}