题目描述:
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.
思路:
时间复杂度O(N),空间复杂度O(N)
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int num : nums)
{
if (set.contains(num))
return true;
else
set.add(num);
}
return false;
}
}