-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirstDuplicate.java
More file actions
39 lines (28 loc) · 1009 Bytes
/
Copy pathFirstDuplicate.java
File metadata and controls
39 lines (28 loc) · 1009 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
38
39
package questions.codesignal.firstduplicate;
import java.util.HashSet;
import java.util.Set;
public class FirstDuplicate {
// Time Complexity: O(1) time or Constant Time
// Space Complexity: O(n) space or Linear Space
public static int firstDuplicate(int[] a) {
// initialize hash set. since it does not hold duplicates
Set<Integer> set = new HashSet<>();
// loop through the array and add each element to the set
for (int j : a) {
// when the element cannot be added to the set the duplicate value is found
if (!set.add(j)) {
return j;
}
}
// there are no duplicates
return -1;
}
public static void main(String[] args) {
int[] a = {2, 1, 3, 5, 3, 2};
System.out.println(firstDuplicate(a));
int[] b = {2, 2};
System.out.println(firstDuplicate(b));
int[] c = {2, 4, 3, 5, 1};
System.out.println(firstDuplicate(c));
}
}