Chef wants to buy a new laptop. However, he is confused about which laptop to buy out of 10 different laptops. He asks his N friends for their recommendation. The i^{th} friend recommends the Chef to buy the ${A_i}^{th}laptop (1 \le A_i \le 10)$.

Chef will buy the laptop which is recommended by maximum number of friends. Determine which laptop Chef buys.
Print CONFUSED if there are multiple laptops having maximum number of recommendations.

EXPLANATION:

Here we can use an unordered map or an array of length 10 to keep count of number of recommendations for each laptop. Once we have the count, we can easily determine which laptop has the maximum recommendations which would be our answer.
Also we have to do one additional check to see if there are more than one laptop with maximum recommendations in which case the answer would be CONFUSED.

Can someone please help , why this code is failing for the problem
n= int(input())
x=[]
dic1={}
while n>0:
m= int(input())
x=list(map(int,input().split()))[:m]
set1=set(x)
for i in set1:
dic1[i]=x.count(i)
val=dic1.values()
maxx=0
a=dic1.values()
maxx=max(a)

result=[]
for i in dic1.keys():
if dic1[i]==maxx:
result.append(i)
if len(result)>1:
print("Confused")
else: print(result[0])
n=n-1

Hey. Can anybody tell me where the logic is going wrong. Couldnâ€™t pass only one test case. In case of CONFUSED scenario, I am comparing last and second last elements in frequency arraylist and if they are equal, the it prints CONFUSED. Here is the code:

int N = scn.nextInt();
int[] arr = new int[N];

HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < N; i++)
{
arr[i] = scn.nextInt();
int freq = map.getOrDefault(arr[i], 0);
map.put(arr[i], freq+1);
}
if (arr.length == 1)
{
System.out.println(arr[0]);
continue;
}
int max_freq = 0;
int maxele = arr[0];
ArrayList<Integer> AL = new ArrayList<>();
for (int i: map.keySet())
{
int val = map.get(i);
if (val > max_freq)
{
max_freq = val;
maxele = i;
}
AL.add(val);
}
Collections.sort(AL);
if (AL.size() != 1 && AL.get(AL.size()-1) == AL.get(AL.size()-2))
{
System.out.println("CONFUSED");
}
else
{
System.out.println(maxele);
}
}

Simply count the mxm hash_value instead of sorting hash_value, as it also takes O(nlogn) time for sorting map which is not fisible for this solution.
if mxm_hash_value more than 1, just print confused, else print mxm_hash_value index.
you can refer my code :

void solve(){
int no_of_friends;
cin>>no_of_friends;
int recommendation_arr[no_of_friends];
int recomm_map[11]={0};
for(int idx=0;idx<no_of_friends;idx++){
cin>>recommendation_arr[idx];
recomm_map[recommendation_arr[idx]]+=1;
}
int mxm_recomm=0,mxm=0;
for(int idx=1;idx<11;idx++){
if(recomm_map[idx]>mxm){
mxm=recomm_map[idx];
mxm_recomm=idx;
}
}
int confused=0;
for(int idx=1;idx<11;idx++){
if(recomm_map[idx]==mxm)
confused++;
}
if(confused>1)
cout<<"CONFUSED\n";
else
cout<<mxm_recomm<<"\n";
}

Hey! Thanks I got your logic and will try to implement it. But why is it failing on one test case? What is the mistake that I am making in this logic. If you can figure it out, it would be great.

Hey Prajjwal! Thanks for helping me out. Why is there a need to typecast though? The maximum possible value will surely fit in an int. I am little confused about it. It would be great if you could help again