<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>集合封装</title>
</head>
<body>
<script>
function Set(){
this.items={}
Set.prototype.add=function(value){
if(this.has(value)){
return false
}
//判断集合中是否又这个元素
this.items[value]=value
return true
}
Set.prototype.has=function(value){
this.items.hasOwnProperty(value)
}
Set.prototype.remove=function(value){
if(!this.has(value)){
return false
}
delete this.items[value]
return true
}
Set.prototype.clear=function(){
this.items={}
}
Set.prototype.size=function(){
return Object.keys(this.items).length
}
Set.prototype.values=function(){
return Object.keys(this.items)
}
Set.prototype.union=function(otherSet){
//创建一个新的集合
var unionSet=new Set()
var values=this.values()
for(var i=0;i<values.length;i++){
unionSet.add(values[i])
}
values=otherSet.values()
for(var i=0;i<values.length;i++){
unionSet.add(values[i])
}
return unionSet
}
Set.prototype.intersection=function(otherSet){
var intersection=new Set()
var values=this.values()
for(var i=0;i<values.length;i++){
unionSet.add(values[i])
if(otherSet.has(item)){
intersection.add(item)
}
}
return intersection
}
Set.prototype.difference=function(otherSet){
var difference=new Set()
for(var i=0;i<values.length;i++){
var item=values[i]
if(!otherSet.has(item)){
difference.add(item)
}
}
return difference
}
}
</script>
</body>
</html>
数据结构83-集合常见操作之差集代码
原创
©著作权归作者所有:来自51CTO博客作者前端导师歌谣的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【数据结构】串的基本操作原来可以这样实现……
【数据结构】第四章——串详细介绍C语言实现串的基本操作……
数据结构 主串 子串 串定位 C语言 -
数据结构75-集合常见操作
前端
学习 前端 es6 -
数据结构83-集合常见操作之子集代码es6 html javascript 前端 学习
-
83-自己实现互斥锁
前面已经讲过两种锁了,现在我们先停一停,思考一ile (tmp == 1) { tmp = *lock; // 如果 *
互斥 linux 互斥量 自旋锁 寄存器