《leetCode》:Contains Duplicate


タイトル
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.

配列に重複する数字がある場合はtrueを返し、そうでない場合はfalseを返します.
構想
セット容器を1つ借りると完成します.
public boolean containsDuplicate(int[] nums) {
        if(nums==null){
            return false;
        }
        Set<Integer> set=new HashSet<Integer>();
        for(int i=0;i<nums.length;i++){
            if(set.add(nums[i])==false){
                return true;
            }
        }
        return false;
    }