uharsh's blog

By uharsh, history, 6 years ago, In English

How can i find the frequency of elements of a given array with space complexity o(1) ??

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 years ago, # |
  Vote: I like it +27 Vote: I do not like it
int a[N];  // array
int n;  // length of array
for (int i = 0; i < n; i++) {
  int cnt = 0;
  for (int j = 0; j < n; j++) {
    if (a[i] == a[j]) ++cnt;
  }
  cout << cnt << endl;  // frequency of i-th element
}