-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1.java
More file actions
37 lines (32 loc) · 978 Bytes
/
Question1.java
File metadata and controls
37 lines (32 loc) · 978 Bytes
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
import java.util.HashSet;
public class Question1 {
public static boolean Distinct(int arr[]) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
return true;
}
}
}
return false;
}
// Time Comlexity=O(n2) Space Complexity=O(1);
// Using HashSet
public static boolean distinct(int[] arr) {
HashSet<Integer> hs = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
if (hs.contains(arr[i])) {
return true;
} else {
hs.add(arr[i]);
}
}
return false;
}
// Time Comlexity=O(n) Space Complexity=O(n);
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 1 };
System.out.println(Distinct(arr));
System.out.println(distinct(arr));
}
}