217. Contains Duplicate (Easy)
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.
Solution 1: HashSet O(n); O(n)
/**
* Solution 1: HashSet O(n); O(n)
* Use HashSet to store each distinct number.
* When adding a number to set returns false, which means duplicate, return true.
*/
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
if (!set.add(num)) {
return true;
}
}
return false;
}
Solution 2: Sort O(nlogn); O(1)
/**
* Solution 2: Sort O(nlogn); O(1)
* Sort the array and then traverse the array to check if exists 2 neighboring numbers are same.
*/
public boolean containsDuplicateB(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}