Skip to content

Commit 7468c93

Browse files
authored
Add files via upload
0 parents  commit 7468c93

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

ContainsDup.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Arrays;
2+
3+
public class ContainsDup {
4+
5+
//Sort and find if next element is same as this element
6+
//Time: O(nlogn)
7+
//Space O(1)
8+
public static boolean containsDuplicate(int[] nums) {
9+
Arrays.sort(nums);
10+
for(int i=0;i<(nums.length-1);i++) {
11+
if(nums[i]==nums[i+1]) {
12+
return true;
13+
}
14+
}
15+
return false;
16+
}
17+
18+
//We can also use a set and add all numbers and check the length to match
19+
//Time: O(n)
20+
//Space: O(n)
21+
22+
public static void main(String[] args) {
23+
System.out.println(containsDuplicate(new int[]{1,2,3,4,6,8,1}));
24+
}
25+
}

0 commit comments

Comments
 (0)