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);
if (nums != null) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
}
return false;
}
}