-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
30 lines (26 loc) · 952 Bytes
/
Solution.py
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
class FirstUnique:
def __init__(self, nums: List[int]):
self.counter = collections.OrderedDict()
self.unique_nums = collections.OrderedDict()
for num in nums:
self.counter[num] = self.counter.get(num, 0) + 1
for k, v in self.counter.items():
if v == 1:
self.unique_nums[k] = 1
def showFirstUnique(self) -> int:
if len(self.unique_nums) == 0:
return -1
for k in self.unique_nums.keys():
return k
def add(self, value: int) -> None:
if value not in self.counter:
self.counter[value] = 1
self.unique_nums[value] = 1
else:
self.counter[value] += 1
if value in self.unique_nums:
self.unique_nums.pop(value)
# Your FirstUnique object will be instantiated and called as such:
# obj = FirstUnique(nums)
# param_1 = obj.showFirstUnique()
# obj.add(value)