Search code examples
arrayscalgorithmfrequency

Finding the highest frequency of an array and all elements which have that frequency


I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?

Format Input: The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.

Format Output: Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.

Constraints: 1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5

Sample Input:

3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1 
4
1 1 1 3

Sample Output:

Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1

Here is my code:

#include <stdio.h>

int main() {
    int T, N[20];

    scanf("%d", &T); getchar();
    
    int A[T][20000];
    
    
    
    for (int i = 0; i<T; i++) {
        scanf("%d", &N[i]); getchar();
        for (int j = 0; j<N[i]; j++) {
            scanf("%d", &A[i][j]); getchar();
        }
        
        int X = 0;
        
        for (int j = 0; j<N[i]; j++) {
            for (int k = j + 1; k<N[i]; k++) {
                if (A[i][k]<A[i][j]) {
                    X = A[i][j];
                    A[i][j] = A[i][k];
                    A[i][k] = X;
                }
            }
        }
    }
    
    int f[20000];
    
    for (int i = 0; i<T; i++) {
        int c = 0, mc = 0;
        
        for (int j = 0; j<N[i]; j++) {
            c = 1;
            if(A[i][j] != -1) {
                for (int k = j+1; k<N[i]; k++) {
                    if (A[i][j] == A[i][k]) {
                        c++;
                        A[i][k] = -1;
                        
                    }   
                }
                
                f[j]=c;
            }   
            if (c>mc) {
                    mc = c;
                }
        }
        printf("Case #%d: %d\n", i+1, mc);
        
        for (int j = 0; j<N[i]; j++) {
            if (A[i][j] != -1) {
                if (f[j] == mc) {
                    printf ("%d", A[i][j]);
                    if (j<N[i]-1) {
                        printf(" ");
                    }
                }
                
            }
            
        }
        
        printf("\n");
        
    }
    
    return 0;
}

EDIT So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.

NEW CODE

#include <stdio.h>

int main() {
    int T, N;

    scanf("%d", &T); getchar();
    
    int A[20000];
    
    for (int i = 0; i<T; i++) {
        scanf("%d", &N); getchar();
        for (int j = 0; j<N; j++) {
            scanf("%d", &A[j]); getchar();
        }
        
        int X;
        
        for (int j = 0; j<N; j++) {
            for (int k = j + 1; k<N; k++) {
                if (A[k]<A[j]) {
                    X = A[j];
                    A[j] = A[k];
                    A[k] = X;
                }
            }
        }
        
        int f[N], c = 0, mc = 0;
        
        for (int j = 0; j<N; j++) {
            c = 1;
            if(A[j] != -1) {
                for (int k = j+1; k<N; k++) {
                    if (A[j] == A[k]) {
                        c++;
                        A[k] = -1;
                    }   
                }
                f[j]=c;
                if (c>mc) {
                        mc = c;
                }
            }   
        }
        printf("Case #%d: %d\n", i+1, mc);
        
        for (int j = 0; j<N; j++) {
            if (A[j] != -1) {
                if (f[j] == mc) {
                    printf ("%d", A[j]);
                    if (j<N-1) {
                        printf(" ");
                    }
                }   
            }   
        }
        printf("\n");
    }
    
    return 0;
}

Solution

  • It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)

    #include <stdio.h>
    
    int main() {
        int T, N;
        
        scanf("%d", &T);
        
        for (int i = 0; i<T; i++) {
            
            scanf("%d", &N); getchar();
            
            //INPUT elements and counting frequncy for each element
            int f[200001] = {0}, E = 0;
            for (int j = 0; j<N; j++) {
                scanf("%d", &E); getchar();
                f[E]++;
            }
            
            //find max frequency and how many elements with max frequency
            int max = 0, c = 0;
            for (int j = 1; j<200001; j++) {
                
                if (f[j] == max) {
                    c ++;
                }
                
                if (f[j]>max) {
                    max = f[j];
                    c = 1;
                }
                
            }
            
            //OUTPUT result
            printf("Case #%d: %d\n", i+1, max);
            int counter = 0;
            for (int j = 1; j<200001; j++) {
                if (f[j] == max) {
                    
                    counter ++;
                    if (counter<c){
                        printf("%d ", j);   
                    } else {
                        printf("%d\n", j);
                    }
                    
                } 
            }
        }
        return 0;
    }