1 parent e3132fa commit afc58b9Copy full SHA for afc58b9
Easy217.java
@@ -0,0 +1,25 @@
1
+//CONTAINS DUPLICATE
2
+import java.util.HashSet;
3
+ import java.util.Set;
4
+
5
+ public class Easy217 {
6
+ public boolean containsDuplicate(int[] nums) {
7
+ // Create a HashSet to store visited elements
8
+ Set<Integer> visited = new HashSet<>();
9
10
+ // Iterate over the array
11
+ for (int i = 0; i < nums.length; i++) {
12
+ // If the element is already in the set, return true
13
+ if (visited.contains(nums[i])) {
14
+ return true;
15
+ }
16
+ // Otherwise, add the element to the set
17
+ else {
18
+ visited.add(nums[i]);
19
20
21
+ // If no duplicates are found, return false
22
+ return false;
23
24
25
0 commit comments