一、问题描述
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.
二、思路
1、对数组升序排序
2、如果有相同的,返回true,否则返回假
3、考虑只有一个元素或者数组为空情况
三、代码
public class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums.length == 0 || nums.length == 1) return false;
Arrays.sort(nums);
for(int i = 1;i < nums.length;++i){
if(nums[i] == nums[i -1]){
return true;
}
}
return false;
}
}