First Unique Number
Description
null
Solution(javascript)
/**
* @param {number[]} nums
*/
var FirstUnique = function(nums) {
this.unique = new Map()
this.map = {}
nums.forEach(num => {
this.map[num] = (this.map[num] || 0) + 1
if(this.map[num] === 1) {
this.unique.set(num, num)
} else {
this.unique.delete(num)
}
})
};
/**
* @return {number}
*/
FirstUnique.prototype.showFirstUnique = function() {
if(this.unique.size > 0) {
return this.unique.values().next().value
} else {
return -1
}
};
/**
* @param {number} value
* @return {void}
*/
FirstUnique.prototype.add = function(num) {
this.map[num] = (this.map[num] || 0) + 1
if(this.map[num] === 1) {
this.unique.set(num, num)
} else {
this.unique.delete(num)
}
};
/**
* Your FirstUnique object will be instantiated and called as such:
* var obj = new FirstUnique(nums)
* var param_1 = obj.showFirstUnique()
* obj.add(value)
*/