Solved programming problem

Counting frequency of each element of 1D array in C.

Source code

#include <stdio.h>

int main()

{

    int array[100], freq[100];

    int n,i,j,count=0;

    printf("Enter size of array: ");

    scanf("%d", &n);

    printf("Enter elements in array: ");

    for(i=0; i<n; i++)

    {

        scanf("%d", &array[i]);

        freq[i] = -1;///saving all the element frequency as -1.

    }

    for(i=0; i<n; i++)

    {

        count = 1;

        for(j=i+1; j<n; j++)

        {

            if(array[i]==array[j]) ///If duplicate element is found

            {

                count++;

                freq[j] = 0; /// Make sure not to count frequency of same element again

            }

        }

        if(freq[i] != 0) /// If frequency of current element is not counted

        {

            freq[i] = count;

        }

    }

    printf("Frequency of all elements of array : 
");

    for(i=0; i<n; i++)

    {

        if(freq[i] != 0)

        {

            printf("%d = %d times
", array[i], freq[i]);

        }

    }

    return 0;

}

Output

Enter size of array: 5

Enter elements in array: 12 34 6 12 34

Frequency of all elements of array :

12 = 2 times

34 = 2 times

6 = 1 times

Join the discussion

Add your comment

3 previous comments

augmentin and vicodin

<a href=https://augmentin.top/>augmentin during pregnancy</a> 76 Other angiogenic candidates include HER2, 77 IGFBP 3, 78 and urokinase type plasminogen activator

lautlew

The analyses were done on SPSS for Windows, version 15 <a href=https://enhanceyourlife.mom/>better business bureau online pharmacy priligy</a> elimite promethazine breastfeeding But young viewers, who can relate to the innocent heroine and her crush on gorgeous Romeo, may not even notice

Squinebib

Buy Amoxicillin Online In Uk Tohnbo [url=https://newfasttadalafil.com/]viagra cialis online[/url] Sonlcu Cialis 20mg Testberichte Dove Acquistare Viagra Generico <a href=https://newfasttadalafil.com/>best place to buy cialis online</a> Can I Use Old Amoxicillin Liquid Iwtvrt https://newfasttadalafil.com/ - Cialis Medicina Levitra Yjmtyu