题目
解答
public void testContainsDuplicate(){
int[] nums = {1,2,3,1};
System.out.println(containsDuplicate(nums));
}
private boolean containsDuplicate(int[] nums){
Map<Integer,Integer> m = new HashMap<Integer, Integer>();
for(int i = 0 ;i<nums.length;i++){
if(m.containsKey(nums[i])){
m.put(nums[i],m.get(nums[i])+1);
}else{
m.put(nums[i],1);
}
}
for(Map.Entry<Integer, Integer> entry : m.entrySet()){
if(entry.getValue()>1) return true;
}
return false;
}