Java for LeetCode 217 Contains Duplicate

时间:2021-03-25 15:36:39

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

解题思路:

HashMap,JAVA实现如下:

    public boolean containsDuplicate(int[] nums) {
HashMap<Integer,Boolean> hm=new HashMap<Integer,Boolean>();
for(int num:nums){
if(hm.containsKey(num))
return true;
hm.put(num, true);
}
return false;
}