题目要求:
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.
即传入一个数组,判断数组中有没有重复的元素
思路:利用HashSet
public class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
for(int i=0;i<nums.length;i++)
{
if(!set.add(nums[i]))
{
return true;
}
}
return false;
}
}