-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_1429.java
More file actions
35 lines (28 loc) · 769 Bytes
/
Copy pathP_1429.java
File metadata and controls
35 lines (28 loc) · 769 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
package leetcode.medium;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("unused")
public class P_1429 {
static class FirstUnique {
int i;
Map<Integer, Integer> seen = new HashMap<>();
List<Integer> q = new ArrayList<>();
FirstUnique(int[] nums) {
for (int num : nums) {
add(num);
}
}
public int showFirstUnique() {
while (i < q.size() && seen.get(q.get(i)) > 1) {
i++;
}
return i == q.size() ? -1 : q.get(i);
}
public void add(int value) {
q.add(value);
seen.merge(value, 1, Integer::sum);
}
}
}