0%
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| class mySet{ constructor(obj){ this.obj = {} this.length = 0 if(Array.isArray(obj)){ for(let i = 0; i < obj.length; i++){ this.obj[this.length] = obj[i] this.length++ } }else if(typeof(obj) === String){ obj = obj.split('') for(let i = 0; i < obj.length; i++){ this.obj[this.length] = obj[i] this.length++ } } }
has(value){ return this.obj.hasOwnProperty(value) }
add(value){ if(!this.has(value)){ this.obj[value] = value this.length++ return this } return false }
del(value){ console.log(this.has(value)); if(this.has(value)){ delete this.obj[value] this.length-- return this } return false }
clear(){ this.obj = {} this.length = 0 return this }
size(){ return this.length }
keys(){ return Object.keys(this.obj) }
values(){ return Object.values(this.obj) }
entries(){ return Object.entries(this.obj) }
union(otherSet){ let newSet = new Set() let values = this.values() for(let i = 0; i < values.length; i++){ newSet.add(values[i]) } values = otherSet.values() for(let i = 0; i < values.length; i++){ if(!this.has(values[i])){ newSet.add(values[i]) } } return newSet }
intersection(otherSet){ let newSet = new Set() let values = this.values for(let i = 0; i < values.length; i++){ if(otherSet.has(values[i])){ newSet.add(values[i]) } } return newSet }
difference(otherSet){ let newSet = new Set() let values = otherSet.values() for(let i = 0; i < values.length; i++){ if(!this.has(values[i])){ newSet.add(values[i]) } } return newSet } }
var myset = new mySet([1, 2, 3])
console.log(myset.union(new mySet([3,4,5])));
var set = new Set() set.add(5) set.add(4) console.log(set);
|