Am I missing something in this MODEFREQ problem

This is my code:

t=int(input())
for i in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    d=[0]*100
    for i in arr:
        d[i]+=1
        
    c=[0]*100
    for j in d:
        if(j!=0):
            c[j]+=1
            
    k=max(c)
    print(c.index(k,0,len(c)))

Not completely sure about python though, but try doing d = [0 for i in range(100)] instead of d = [0]*100

It happens because when declare using the later, python makes 100 “Identical” copies of [0], so any change in one happens in each of them.

1 Like

Why 100? Freq of any element can go upto 10000.

1 Like

Thank you for replying! And isn’t that the same thing? It makes a List of 100 ‘0s’ to be precise.

Ah right! I tried the same thing yesterday but it was giving me a run time error. Another question would you recommend me using a dictionary over list for such a question?

When you make [0]*100, you are actually creating the same 0, 100 times. So if you change value at one index, that will change the value at each index, as they are the same objects.

Ah, no no its not giving that error, all elements are uniquely addressed in the list.

Yes
That’s RTE because of out of bounds.

Have you tried printing the array, is it working properly?

I don’t have much experience with python so I might be wrong but can you try using different variable name for both loops.

You’ve use i for both the main test case loop and for the Freq calculation. I don’t know if that matters though.

If you look at his profile, he has already done this problem and the actual issue is highlighted in my previous replies.