Title description
Given an integer array, determine whether there are duplicate elements.
If there is a value that appears at least twice in the array, the function returns true. If each element in the array is different, then return false.
Ideas
- Sort the array first (Arrays.sort(XX))
- Determine whether adjacent elements are equal
Code
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;
}
}