uharsh's blog

By uharsh, history, 3 months ago, In English,

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

 
 
 
 

»
3 months 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
}