# Why my solution is wrong?who can help me?

• ``````{
HashMap<Integer,Integer> mymap= new HashMap<Integer,Integer>();
int count=0;
for(int i=0;i<num.length;i++){
if(!mymap.containsKey(num[i])){
count=1;
mymap.put(num[i],count);
}
else{
mymap.put(num[i],count++);
}
}
int max=0;
int result=0;
Set key = mymap.keySet();
Iterator ite=key.iterator();
while(ite.hasNext()){
int name = Integer.parseInt(ite.next().toString());
int count2 = Integer.parseInt(mymap.get(name).toString());
if(max<count2){
max=count2;
result=name;
}
}
return result;
}``````

• The problem is you use

``````int count=0;
``````

so count changes for all the cases.
You can refactor the code use

``````            if(!mymap.containsKey(num[i])){
mymap.put(num[i], 1);
}
else{
int count = mymap.get(num[i]) + 1;
mymap.put(num[i], count);
}
``````

to get value stored for the key and add 1 to the value then put it to the key again.

• em.. I got your meanings,thank you very much!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.