class Solution {
public boolean containsDuplicate(int[] nums) {
HashMap hashMap = new HashMap();
for (int i = 0; i < nums.length; i++) {
Object put = hashMap.put(nums[i], nums[i]);
if (put!=null) {
return true;
}
}
return false;
}
}
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
//因为集合set中不能有重复的元素,如果有重复的
//元素添加,就会添加失败
if (!set.add(num))
return true;
}
return false;
}
作者:数据结构和算法
链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x248f5/?