Skip to content

Commit b396769

Browse files
contains duplicate
1 parent ab33bf1 commit b396769

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

EASY/src/easy/ContainsDuplicate.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package easy;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 217. Contains Duplicate QuestionEditorial Solution My Submissions Total Accepted: 106831 Total
8+
* Submissions: 252115 Difficulty: Easy Given an array of integers, find if the array contains any
9+
* duplicates. Your function should return true if any value appears at least twice in the array,
10+
* and it should return false if every element is distinct.
11+
*/
12+
public class ContainsDuplicate {
13+
public boolean containsDuplicate(int[] nums) {
14+
if(nums == null || nums.length == 0) return false;
15+
Set<Integer> set = new HashSet();
16+
for(int i : nums){
17+
if(!set.add(i)) return true;
18+
}
19+
return false;
20+
}
21+
22+
public static void main(String...strings){
23+
int[] nums = new int[]{1,2,3,4, 3};
24+
ContainsDuplicate test = new ContainsDuplicate();
25+
System.out.println(test.containsDuplicate(nums));
26+
}
27+
}

0 commit comments

Comments
 (0)