-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFirstUnique.js
46 lines (39 loc) · 859 Bytes
/
FirstUnique.js
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
/**
* @param {number[]} nums
*/
const FirstUnique = function (nums) {
this.unique = new Set()
this.duplicate = new Set()
for (let i = 0; i < nums.length; i++) {
const num = nums[i]
this.add(num)
}
}
/**
* @return {number}
*/
FirstUnique.prototype.showFirstUnique = function () {
if (this.unique.size === 0) {
return -1
}
return this.unique.values().next().value
}
/**
* @param {number} value
* @return {void}
*/
FirstUnique.prototype.add = function (value) {
if (!this.unique.has(value) && !this.duplicate.has(value)) {
this.unique.add(value)
return
}
this.unique.delete(value)
this.duplicate.add(value)
}
/**
* Your FirstUnique object will be instantiated and called as such:
* var obj = new FirstUnique(nums)
* var param_1 = obj.showFirstUnique()
* obj.add(value)
*/
module.exports = FirstUnique