-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontains-duplicate.java
38 lines (35 loc) · 1.01 KB
/
contains-duplicate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// https://leetcode.com/problems/contains-duplicate/
// link for problem
// BRUTE FORCE SOLUTION
class Solution {
public boolean containsDuplicate(int[] nums) {
for(int i =0;i<nums.length-1;i++){
for(int j = i+1;j<nums.length;j++){
if(nums[i]==nums[j]){
return true;
}
}
}
return false;
}
}
//OPTIMISED SOLUTION 1 (USING HASHSET DATASET)
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> map = new HashSet<>();
for(int i = 0;i<nums.length;i++){
if(map.contains(nums[i]))return true; map.add(nums[i]);
}
return false;
}
}
// OPTIMISED SOLUTION 2 (USING SORTING ARRAYS METHOD)
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for(int i =0;i<nums.length-1 ;i++){
if(nums[i] == nums[i+1]) return true;
}
return false;
}
}