0

By looping, I am trying to determine unique and duplicate elements in a given array and display their corresponding frequency. My code is as following:

    #include <stdio.h>
#define Frequency_size 10000
#define N 20
void unique_numbers(int array[], int n){
   int i,j;
   int count = 0;
   printf("List of unique numbers: ");
   for(i = 0; i < n; i++){
      for(j = 0; j < n; j++){
         if(array[i] == array[j] && i != j)
         break;
      }
      
      if(j == n ){
         printf("%d ",array[i]);
         ++count;
      }
   }
   printf("\n");
   printf("Element count in List of Unique Numbers = %d", count);
}


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

    int frequency[Frequency_size] = { 0 };
printf("List of Duplicated Numbers: \n");

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

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

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

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

    printf("Element Count in List of Duplicated Numbers = %d",totdup);
    
}


int main(){
    int array[]={41, 47, 23, -62, -52, 15, 41, -88, 90, -62, -40, 37, 34, 88, 26, -54, 53, 15, 41, 46};
    unique_numbers(array, N);
    printf("\n\n\n\n");
    non_unique_numbers(array, N);
}

When I run this my output is:

List of unique numbers: 47 23 -52 -88 90 -40 37 34 88 26 -54 53 46 
Element count in List of Unique Numbers = 13



List of Duplicated Numbers:
Number: 15  , Frequency: 2
Number: 41  , Frequency: 3
Element Count in List of Duplicated Numbers = 2

But in my array, I also have -62 duplicate 2 times. I also want to display that but I could not manage to handle negative numbers.

Desired output is:

List of Duplicated Numbers:
Number: 15  , Frequency: 2
Number: 41  , Frequency: 3
Number: -62  , Frequency: 2
Element Count in List of Duplicated Numbers = 3

It is because of the for loop of course but I could not manage to modify it. What can I do? I only want to use for loops by the way.

5
  • 1
    frequency[arr[i]] is an element of frequency at index arr[i]. arr[3] == -62. Then that code would look like ++frequency[-62];. What would a negative index mean? Commented Nov 24, 2021 at 18:09
  • What is Frequency_size Where does it come from? What is N? The code is very incomplete. Commented Nov 24, 2021 at 18:13
  • I did not post all of the code since some parts are irrelevant. N is 20 which is the size of the array Commented Nov 24, 2021 at 18:15
  • Why would you expect to get output -62 last? Commented Nov 24, 2021 at 18:24
  • Order is not important. I don't expect it to be last of course. Commented Nov 24, 2021 at 18:26

1 Answer 1

2

Assuming that Frequency_size relates to the maximum absolute value of any number that can be stored in the array, you could try this:

int frequency_array[2*Frequency_size + 1] = { 0 };
int *frequency = &frequency_array[Frequency_size];

// Now you can access frequency[x] from -Frequency_size .. 0 .. +Frequency_Size

printf("List of Duplicated Numbers: \n");

for (i = 0; i < size; i++)
{
  if (arr[i] >= -Frequency_size && arr[i] <= Frequency_size)
  {
    ++frequency[arr[i]];
  }
}

for (i = -Frequency_size; i <= Frequency_size ; i++) 
{
  // .. Do the printing stuff here
}
Sign up to request clarification or add additional context in comments.

2 Comments

I changed the question and completed the code
Number of elements in this array is fixed which is 20. I do not need to change it. However to count and display frequencies I store this information in another array. Since I did not know the frequency size for each number I just gave a maximum number say 1000.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.