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搞定。
AC Code
public class Solution { public boolean containsDuplicate(int[] nums) { SetappearedNum = new HashSet (); for(int i = 0; i < nums.length; i++){ if(!appearedNum.contains(nums[i])){ appearedNum.add(nums[i]); } else return true; } return false; } }