Help me in solving LAPTOPREC problem

My issue

My code

# cook your dish here
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    m=[]
    for i in l:
        m1=l.count(i)
        if m1>=2:
            m.append(i)
    f=set(m)      
    if len(f)<2:
        print(*f)
    else:
        print("CONFUSED")

Problem Link: LAPTOPREC Problem - CodeChef

@siddu_410

In the given problem, we have to find the option which is most recommended. The logic I used to solve the problem was like this;

First, I created a set from the values of list a. Then I initialized three variables, namely;

  • val to store the option with maximum occurrence.
  • c as a counter variable to store maximum number of occurrence.
  • flag to check if there if there are more than one value with maximum occurrence.

Now, iterate over the set b. Store number of occurrence of the current value in a temporary variable, t. Check if it is more than c or not, if yes, update c and val.

Now to check if there exists another option with maximum value, I iterate again over set b. If the option has occurrence equals to c and is not val, update the flag to 0.

Finally, check the value of flag, if it’s equal to zero, print CONFUSED, otherwise print the value stored in val.

# cook your dish here
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=set(a)
    val=0
    c=-1
    flag=1
    for i in b:
        t=a.count(i)
        if(t>c):
            c=t
            val=i
    for i in b:
        if((a.count(i)==c)and(i!=val)):
            flag=0

    if(flag==0):
        print('CONFUSED')
    else:
        print(val)