Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

More than one duplicated element in C

Let’s say I have an array like this:

int array[]={1, 2, 2, 1, 2, 4, 4, 5};

I want to display all duplicate elements only once, display their corresponding frequency and count total duplicate elements. I searched about it a lot but every solution is for an array that has only 2 duplicate elements.

void non_unique_numbers(int arr[], int size){
    int i,j;
    int frequency[Frequency_size]={0};
    for(i=0; i<size; i++){
        for(j=0; j<size; j++){
            if(arr[i]==arr[j] && i!=j){
                ++frequency[arr[j]];
                printf("Number: %d  , Frequency: %d\n", arr[j], frequency[j]);
            }
        }
    }
}

When I run this my output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Number: 1  , Frequency: 3
Number: 2  , Frequency: 1
Number: 2  , Frequency: 1
Number: 2  , Frequency: 1
Number: 2  , Frequency: 2
Number: 1  , Frequency: 0
Number: 2  , Frequency: 2
Number: 2  , Frequency: 6
Number: 4  , Frequency: 0

which is meaningless. How can I display every duplicate element once and find out their corresponding frequency?

>Solution :

Rather than nested loops, you need two loops.

One to calculate the frequency. And, another that displays the frequencies and calculates the total number of duplicate numbers.

Here is the refactored code:

#include <stdio.h>

#define Frequency_size      10000

void
non_unique_numbers(int arr[], int size)
{
    int i;
    int freq;
    int totdup = 0;

    int frequency[Frequency_size] = { 0 };

    // calculate frequency
    for (i = 0; i < size; i++)
        ++frequency[arr[i]];

    for (i = 0; i < size; i++) {
        freq = frequency[arr[i]];
        printf("Number: %d  , Frequency: %d\n", arr[i], freq);
        if (freq >= 2)
            ++totdup;
    }

    printf("The number of duplicated numbers is: %d\n",totdup);
}

UPDATE:

Yes but it still displays the same element more than once –
goku

Oops, my bad … Here is one that indexes into the frequency table on the second loop:

#include <stdio.h>

#define Frequency_size      10000

void
non_unique_numbers(int arr[], int size)
{
    int i;
    int freq;
    int totdup = 0;

    int frequency[Frequency_size] = { 0 };

    // calculate freqency
    for (i = 0; i < size; i++)
        ++frequency[arr[i]];

    for (i = 0; i < Frequency_size; i++) {
        freq = frequency[i];
        if (freq <= 0)
            continue;

        printf("Number: %d  , Frequency: %d\n", i, freq);

        if (freq >= 2)
            ++totdup;
    }

    printf("The number of duplicated numbers is: %d\n",totdup);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading