Monday 21 August 2017

Frequency of Array Elements

Given an unsorted array of N integers which can contain integers from 1 to N. Some elements can be repeated multiple times and some other elements can be absent from the array. Count frequency of all elements from 1 to N.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an integer 'N' denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
For each test case output N space-separated integers denoting the frequency of each element from 1 to N.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 100
Example:
Input:
2
5
2 3 2 3 5
4
3 3 3 3
Output:
0 2 2 0 1
0 0 4 0
                                     C-Solution
#include <stdio.h>
#include <stdlib.h>
int main() 
{
    int t,n,i;
    scanf("%d",&t);
while(t--)
{
   scanf("%d",&n);
   int a[n],*b=(int *)calloc(n+1,sizeof(int));
   for(i=0;i<n;i++)
   {
    scanf("%d",&a[i]);++b[a[i]];//printf("%d=%d",a[i],b[a[i]]);
    }
//printf("\n");
   for(i=1;i<=n;i++)
  {
   printf("%d ",b[i]);
  }printf("\n");
}
    return 0;
}

No comments:

Post a Comment