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

Handling negative numbers in array

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.

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

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.

>Solution :

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] = { 0 };
int *frequency = &frequency_array[Frequency_size];

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

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
}
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