hey can any one try out this problem....
1)Write a program to read a sequence of N integers and print the number that appears the maximum number of times in the sequence.

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].

Recommended Answers

All 13 Replies

What have you done so far? Do you know how to read a file? If not, then you need to read tutorials on ifstream (input file stream) and ofstream (output file stream).

i want the code for this program using arrays

i want the code for this program using arrays

Then write it. Also, please read our rules concerning homework questions without proof of effort. We won't do these problems for you, but we'll help if you get stuck doing it yourself.

I have done it myself...but im not getting the required output though it is getting compiled..

post the program so we can see what you did.

// Same question as asked by pooja 4
// plese help me guys i have tried my best but not getting desired result

#include<stdio.h>

int main()
{
int n,a[10],i,j,count;
scanf("%d",&n);
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
    {
        if(a[i]==a[j])
        {
            count=a[i];
        }
    }
}
printf("%d",count);
     return 0;
}

In lines 14-23 you need to count how many times each number appears in the array. For example if the array contains 1 2 3 1 2 3 1 4 5 there are three 1s, two 2s, two 3s, and one each 4 and 5. So the 1s win. There are several ways to do that, probably the simplest is to use a structure that contains the value in the array and the count of the number of times that value appears in the array. Another way to do it is to use two arrays instead of structure. Array1 contains the value that is in the original array and array2 is the count of the number of times the value in array1 appears in the original array

The two arrays will contain the following values

1  3
2  2
3  2
4  1
5  1

You will have to loop through the original array. For each element in the oritinal array
check if the value is already in Array1
if it is already in Array1 then increment the corresponding element in Array2
If it is NOT in Array1 then add it and set it's count in Array2 to 1.

Replace "scanf("%d",&a[i]);" with " scanf("%d",&a[i][j]); " that's it ....

No. array a is a one-dimensional array so a[i][j] won't work.

oh..yup bro...well this is your desired code chandan...may be i think so

#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;
}
commented: it worked! +0

thanks you people!
@vijay kumar its worked dude \m/ thanks a lot!
and thanks a lot @ancient dragon for explaining in my behalf!

You really don't need that large if statement, just use the mod operator and replace lines 9-12 with just this one statement.

++counter[abs(input) % 200];

this ques was asked in NPTEL online course

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.