集合是由一组无序且唯一(不能重复)的项组成的。这个数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中。 在数学中,集合是一组不同的对象(的集)。
// 使用集合
class Set{
// 创建一个空合集
constructor(){
this.item = {}
}
// 判断是否包含
has(value){
return this.item.hasOwnProperty(value)
}
// 添加
add(value){
if(!this.has(value)){
this.item[value]= this.item
return true
}
return false
}
// 删除
remove(value){
if(this.has(value)){
delete this.item[value]
}
}
// 大小
get size(){
return Object.keys(this.item).length
}
// 数值
get value(){
return Object.keys(this.item)
}
}