public class Solution {
public boolean containsDuplicate(int[] nums) {
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int b: nums){
if(b<min){
min=b;
}
if(b>max){
max=b;
}
}
boolean a[] = new boolean[maxmin+1];
for(int b: nums){
if(a[bmin]){
return true;
}else{
a[bmin]=true;
}
}
return false;
}
}
3ms Java solution beats 98% of the answers


I assume by "a Set approach" you mean while looping thru the nums array, if the Set does not contains the current element > add it, else return true. The complexity for this approach is O(N^2). The solution above has the complexity of O(2N) > O(N) but its space complexity could be as big as the gap between max and min values in the array.

said in 3ms Java solution beats 98% of the answers:
int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; for(int b: nums){ if(b<min){ min=b; } if(b>max){ max=b; } } boolean a[] = new boolean[maxmin+1]; for(int b: nums){ if(a[bmin]){ return true; }else{ a[bmin]=true; } } return false;
This is cheating leetcode without enough test case.
e.g.
[2147483648, 2147483647]
