LeetCode: Contains Duplicate

LeetCode: Contains Duplicate

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ContainsDuplicate {

public boolean containsDuplicate(int[] nums) {
if (nums == null || nums.length <= 0) {
return false;
}
Set<Integer> set = new HashSet<Integer>();
for (int i : nums) {
if (set.contains(i)) {
return true;
} else {
set.add(i);
}
}
return false;
}
}