标签:cat [] should false ever integer dup hashset ica
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 {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for(int i=0;i<nums.length-1;i++){
if(nums[i]==nums[i+1]) return true;
}
return false;
}
}
另外一种解法:
public class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for(int i=0;i<nums.length;i++){
if(set.contains(nums[i])){
return true;
}
set.add(nums[i]);
}
return false;
}
}
标签:cat [] should false ever integer dup hashset ica
原文地址:http://www.cnblogs.com/codeskiller/p/6358745.html