Solution:
Use a HashSet to store the one we checked.
If the number is in the HashSet, we found the duplicate and return true.
The time complexity is O(n) and space complexity is O(n) as well.
Code:
public class Solution { public boolean containsDuplicate(int[] nums) { if (nums == null || nums.length == 0) { return false; } HashSet<Integer> hash = new HashSet<>(); for (int i = 0; i < nums.length; i++) { if (hash.contains(nums[i])) { return true; } hash.add(nums[i]); } return false; } }