Question

I'm doing an online course on "Programming, Data Structure & Algorithm". I've been given an assignment to "find the most frequent element in a sequence using arrays in C (with some constraints)". They've also provided some test-cases to verify the correctness of the program. But I think I'm wrong somewhere.

Here's the complete question from my online course.

INPUT

Input contains two lines. First line in the input indicates N, the number of integers in the sequence. Second line contains N integers, separated by white space.

OUTPUT

Element with the maximum frequency. If two numbers have the same highest frequency, print the number that appears first in the sequence.

CONSTRAINTS

1 <= N <= 10000

The integers will be in the range

[-100,100].

And here's the test cases.

Test Case 1

Input:

5
1 2 1 3 1

Output:

1

Input:

6
7 7 -2 3 1 1

Output:

7

And here's the code that I've written.

#include<stdio.h>

int main()
{
    int counter[201] = {0}, n, i, input, maximum = 0;
    scanf("%d", &n);
    for(i = 1; i <= n; i++) {
        scanf("%d", &input);
        if(input < -100 && input < 100)
            ++counter[input];
    }
    maximum = counter[0];
    for (i = 1; i < 201; i++) {
        if (counter[i] > maximum) {
            maximum = counter[i];
        }
    }
    printf("%d", maximum);

    return 0;
}

Please tell me where I'm wrong. Thank you.

EDIT:

I've modified the code, as suggested by @zoska. Here's the working code.

#include<stdio.h>

int main()
{
    int counter[201] = {0}, n, i, input, maximum = 0;
    scanf("%d", &n);
    for(i = 1; i <= n; i++) {
        scanf("%d", &input);
        if(input < 100 && input > 0)
            ++counter[input + 100];
        else
            ++counter[input];
    }
    maximum = counter[0];
    for (i = 0; i < 201; i++) {
        if (counter[i] > maximum) {
            maximum = i - 100;
        }
    }
    printf("%d", maximum);

    return 0;
}
Was it helpful?

Solution

Additionally to problem pointed out by Paul R is:

You are printing maximum occurrences of number, not the number itself.

You're going to need another variable, which will store the number with maximum occurences. Like :

maximum = count[0];
int number = -100;
for (i = 0; i < 201; i++) {
    if (counter[i] > maximum) {
        maximum = counter[i];
        number = i - 100;
    }
}
printf("number %d has maximum occurences: %d", number, maximum);

Also you should iterate through an array from 0 to size-1: So in all cases of your loops it should be :

for(i = 0; i < 201; i++)

Otherwise you won't be using count[0] and you will only have a range of -99...100.

OTHER TIPS

Try below code

#include<stdio.h>

int main()
{
    int counter[201] = {0}, n, i, input, maximum = 0;
    scanf("%d", &n);
    for(i = 1; i <= n; i++) {
        scanf("%d", &input);
        if(input >= -100 && input <= 100)
            ++counter[input + 100];
    }
    maximum = counter[0];
    int index = 0;
    for (i = 0; i < 201; i++) {
        if (counter[i] >= maximum) {
        index = i;
        maximum = counter[i];
        }
    }
    printf("number %d occured %d times\n", index-100, maximum);

    return 0;
}

I would prefer checking in one loop itself for the maximum value just so that the first number is returned if i have more than one element with maximum number of occurances. FInd the code as:

#include<stdio.h>
int main()
{
    int n,input;
    scanf("%d",&n);
    int count[201] ={0};
    int max=0,found=-1;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&input);
        count[input+100]++;
        if(max<count[input+100])
     {
            max= count[input+100];
            found=input;
     }
    }
    printf("%d",found);
    return 0;
}

But, there is also one condition that if the number of occurance are same for two numbers then the number which appers first in sequence should appear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top