集合
集合是由一组无序且唯一(即不能重复)的项组成的。
class Set { constructor(){ this.items = {} } } Set.prototype.has = function(element){ // return element in this.items return Object.prototype.hasOwnProperty.call(this.items,element) } Set.prototype.add = function(element){ if(!this.has(element)){ this.items[element] = element return true } return false } Set.prototype.delete = function(element){ if(this.has(element)){ delete this.items[element] return true } return false } Set.prototype.clear = function(){ this.items = {} } Set.prototype.size = function(){ return Object.keys(this.items).length } Set.prototype.values = function(){ return Object.values(this.items).length }
- 并集:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合。
- 交集:对于给定的两个集合,返回一个包含两个集合共有元素的新集合。
- 差集:对于给定的两个集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。
- 子集:验证一个给定集合是否是另一个集合的子集。
以自己现在的努力程度,还没有资格和别人拼天赋