用哈希表保存出现的次数即可。
public class Solution {
public int majorityElement(int[] nums) {
int max=0,result=0,curLen=0;
HashMap<Integer,Integer> ha=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++)
{
if(!ha.containsKey(nums[i]))
ha.put(nums[i], 1);
else
{
curLen=ha.get(nums[i]);
curLen++;
ha.put(nums[i], curLen);
}
curLen=ha.get(nums[i]);
if(curLen>max)
{
max=curLen;
result=nums[i];
}
}
return result;
}
}