Answer to Question #199310 in C for Prasanna

Question #199310

From a set of array elements, find the number of occurrences of each element present in the given array


Input:

5

2

4

2

5


Output


2 occurs 2 time


4 occurs 1 time


5occurs 2 time


1
Expert's answer
2021-05-27T19:09:47-0400
#include <stdio.h>
int main()
{
    int num,i,j;
    printf("Enter the number of elements in the array : ");
    scanf("%d",&num);
    int arr[num];
    printf("Enter the elements of the array : ");
    for(i=0;i<num;i++)
    {
        scanf("%d",&arr[i]);
    }
    int frequency[num];
    int visited = -1;


    for(i = 0; i < num; i++)
    {
        int count = 1;
        for( j = i+1; j < num; j++)
        {
            if(arr[i] == arr[j])
            {
                count++;
                frequency[j] = visited;
            }
        }
        if(frequency[i] != visited)
        {
            frequency[i] = count;
        }
    }
    printf("Element  Frequency\n");
    for(i = 0; i < num; i++)
    {
        if(frequency[i] != visited)
        {
            printf("%d \t : %d \n", arr[i], frequency[i]);
        }
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS