目录
1. 题目描述
2. 解题思路
哈希表不愧是刻在DNA中的记忆了,看到这道题第一反应又是想起用哈希表解决问题,其实也可以通过排序的方法来检测重复元素,不过考虑到排序的时间复杂度较高,比不上哈希表那么简洁,遂作罢了。
3. 代码实现
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> integers = new HashSet<>();
for (int num : nums) {
if(integers.contains(num))
return true;
integers.add(num);
}
return false;
}