Description
Given an integer array nums
, return true
if any value appears more than once in the array, otherwise return false
.
Example 1:
Input: nums = [1, 2, 3, 3]
Output: true
Example 2:
Input: nums = [1, 2, 3, 4]
Output: false
Code
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> arraySet = new HashSet();
for(int c: nums) {
if(arraySet.contains(c)) return true;
arraySet.add(c);
}
return false;
}
}
Time & Space Complexity
- Time complexity: O(n)
- Space complexity: O(n)