-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstUnique.java
48 lines (40 loc) · 1.26 KB
/
FirstUnique.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
39
40
41
42
43
44
45
46
47
48
package techQuestions;
import java.util.*;
public class FirstUnique {
public Set<Integer> allAdded = new HashSet<Integer>();
public Set<Integer> uniqueOnly = new LinkedHashSet<Integer>();
public FirstUnique(int[] nums) {
for (int element : nums) {
add(element);
}
}
public int showFirstUnique() {
// our unique set is completely empty!
if (uniqueOnly.isEmpty()) {
return -1;
}
return uniqueOnly.iterator().next();
}
public void add(int value) {
// we know that an add on a set will fail if
// the element already exists!
if (allAdded.add(value)) {
uniqueOnly.add(value);
} else {
// this value is in allAdd! remove it from unique set
uniqueOnly.remove(value);
}
}
public static void main(String[] args) {
// let me test this theory of truthiness for a set.add() ...
// Set<Integer> test = new HashSet<Integer>();
// System.out.println(test.add(5));
// System.out.println(test.add(5));
}
}
/**
* Your FirstUnique object will be instantiated and called as such:
* FirstUnique obj = new FirstUnique(nums);
* int param_1 = obj.showFirstUnique();
* obj.add(value);
*/