217. Contains Duplicate Java Solutin

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.

Subscribe to see which companies asked this question

public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if(nums == null || nums.length == 1)
            return false;
        HashSet<Integer> hs = new HashSet<Integer>();
    // Set<Integer> hs = new HashSet<Integer>(); 最初使用Set超时,         //遂使用HashSet
        for(int i=0;i<nums.length;i++){
            if(hs.contains(nums[i]))
                return true;
            else{
                hs.add(nums[i]);
            }
        }
        return false;
    }
}
原文地址:https://www.cnblogs.com/guoguolan/p/5386539.html