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

Find how many times numbers appear in array

My program should print how many times each number appears in array.

OUTPUT:

Number 1 appears 2 times

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 2 appears 5 times

Number 3 appears 1 times

Number 4 appears 3 times

Most frequent: 2

This is my output:

Number 1 appears 1 times
Number 2 appears 4 times
Number 1 appears 0 times
Number 4 appears 2 times
Number 2 appears 3 times
Number 4 appears 1 times
Number 2 appears 2 times
Number 2 appears 1 times
Number 3 appears 0 times
Number 4 appears 0 times
Number 2 appears 0 times
Most frequent: 2
#include <stdio.h>

void main() {
    int i, j, count = 0, max_count = 0, max, n;
    int arr[] = {1, 2, 1, 4, 2, 4, 2, 2, 3, 4, 2}, counter[1000] = {0};
    n = sizeof(arr) / sizeof(*arr);

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

        for (j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
                count++;
                counter[i]++;
            }
            if (count > max_count) {
                max = arr[j];
                max_count = count;
            }
        }
    }

    for (i = 0; i < n; i++)
        printf("Number %d appears %d times\n", arr[i], counter[i]);

    printf("Most frequent: %d\n", max);
}

Could you help me fix my code?

>Solution :

Try this (see below). One of your main problems is that you are using counter[i] but that is using a loop index, not the actual number from the array. So you need to use counter[arr[i]]. The other problem is that you are using two nested loops, which will double up your counts.

Note: The quicksort compare function is taken from this answer.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int compare( const void* a, const void* b)
{
     int int_a = * ( (int*) a );
     int int_b = * ( (int*) b );

     if ( int_a == int_b ) return 0;
     else if ( int_a < int_b ) return -1;
     else return 1;
}

int main()
{
  int i, j, count = 0, max_count = 0, max, n;
  int arr[] = {1, 2, 1, 4, 2, 4, 2, 2, 3, 4, 2}, counter[1000] = {0};
  n = sizeof(arr) / sizeof(*arr);
  qsort( arr, n, sizeof(int), compare );
  for (i = 0; i < n; i++) {
    ++counter[arr[i]];
  }
  max = 0;
  for (i = 0; i < n; i++) {
    if (counter[arr[i]] > max) {
      max = arr[i];
    }
  }
  
  int seen[1000];
  memset(seen,0,sizeof seen);
  for (i = 0; i < n; i++) {
    if (seen[arr[i]])
      continue;
    seen[arr[i]] = 1;
    printf("Number %d appears %d times\n", arr[i], counter[arr[i]]);
  }
  printf("Most frequent: %d\n", max);
}

Output:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c C:\Temp\temp.exe
Number 1 appears 2 times
Number 2 appears 5 times
Number 3 appears 1 times
Number 4 appears 3 times
Most frequent: 4

> Terminated with exit code 0.
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