1320. 包含重复值么

  1. 包含重复值么
  2. Contains Duplicate

描述

给定一个整数数组,查找数组是否包含任何重复项。 如果数组中某个值至少出现两次,则函数应返回true,如果每个元素都是不同的,则返回false。

样例

给定nums =[1,1],返回true

Description
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.
public class Solution {
    /**
     * @param nums: the given array
     * @return: if any value appears at least twice in the array
     */
	   public static boolean containsDuplicate(int[] nums) {
	        // Write your code here
            Set<Integer> set = new HashSet<>();
            for(Integer i : nums){
                if( !set.add(i)){
                    return true;
                }
                
            }
            return false;
            
	        
	    }
}

posted @ 2019-04-02 21:48  故人叹  阅读(88)  评论(0)    收藏  举报