This question is part of NeetCode150 series.
Problem Description
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
leetcode
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int i=0; i<nums.length; i++){
if(set.contains(nums[i])) return true;
set.add(nums[i]);
}
return false;
}
}