Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (rbx):

Looking for a hint [general programming/C]. I have to find most frequent element in a sequence which is fairly easy, the problematic part is 'If two numbers have the same highest frequency, print the number that appears first in the sequence.'

OpenStudy (rbx):

I could create an array and store all inputs but the constraints on input size are ```java 1 <= N <= 10000 The integers will be in the range [-100,100]. ``` and I do not want to create an array sized 10000.

OpenStudy (rbx):

This is the solution with array size 10000, and as can be seen - with low number of inputs (say 10) ~99% of array remains unused.

OpenStudy (anonymous):

You could say start with -100, go through the array and count all the times -100 shows up. Store the count in a variable say highest_count, then do it for -99 and if it appears more than -100, overwrite that variable. And do that till 100. And to help with the same highest frequency, when you're checking to see if the next number is equal or greater to the previous count you can create a condition to store the value in say second_highest if the values are equal. That way the first highest is in the first variable.

OpenStudy (rbx):

That still would require storing all the inputs (and thus an array sized 10000 integers). And what is the point of calculating frequency later? If there actually are 10000 inputs I might have to go through all the 10000 inputs 200 times. And still more importantly, how does this tell which number was entered first (without going through input array yet again)?

OpenStudy (anonymous):

So you don't want to store the input? Or you just don't want to make an array size 10k? If you don't want to make a 10k size array just make it dynamic then and use malloc and realloc to grow the array as you need it.

OpenStudy (anonymous):

try using break as soon as find ur first largest number use break.

OpenStudy (anonymous):

if(mode==max) { printf("%d",a[i]); break;}

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!