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.

O(N2) time O(1) memory, TLE, cited from Leetcode Discussion Board

1
2
3
4
5
6
7
8
9
10
public boolean containsDuplicate(int[] nums) {
for(int i = 0; i < nums.length; i++) {
for(int j = i + 1; j < nums.length; j++) {
if(nums[i] == nums[j]) {
return true;
}
}
}
return false;
}

O(N log N) time O(1) memory, TLE, cited from Leetcode Discussion Board

1
2
3
4
5
6
7
8
9
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for(int ind = 1; ind < nums.length; ind++) {
if(nums[ind] == nums[ind - 1]) {
return true;
}
}
return false;
}

O(N) time O(N) memory

1
2
3
4
5
6
7
8
9
10
11
public class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < nums.length; i++){
if (!set.add(nums[i])){
return true;
}
}
return false;
}
}