集合的封装与实现

集合的元素有两个特征,一是无序,二是不可重复。

JavaScript内置集合对象。 new Set(Array [])

const log = console.log
//集合 -元素无序,不可重复。
class MySet {
   items = {}
   
  add(value) {
    if(this.has(value)) return false
    let keyName = value + ''
    this.items[keyName] = value
    return true
  }

  has(value) {
    return this.items.hasOwnProperty(value)
  }

  remove(value) {
    if(!this.has(value)) return false
//     log(this.items[value],this.items.value)
    delete this.items[value]
    return true
  }

  clear() {
    this.items = {}
  }

  size() {
    return Object.keys(this.items).length
  }

  values() {
    return Object.keys(this.items)
  }
   
}

let newset = new MySet()
newset.add(1)
newset.add(2)
newset.add("2")
newset.add(3)
newset.remove(3)
newset.add('4599')
log(newset.has(4599))
log(newset.size())
// newset.clear()
log(newset.values())

复制成功
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

集合间的操作

求该集合与其他集合的并集,交集,验证是否为对方集合的子集

//集合间的并集
const log = console.log
//集合 -元素无序,不可重复。
class MySet {
   items = {}
   
  add(value) {
    if(this.has(value)) return false
    let keyName = value + ''
    this.items[keyName] = value
    return true
  }

  has(value) {
    return this.items.hasOwnProperty(value)
  }

  remove(value) {
    if(!this.has(value)) return false
//     log(this.items[value],this.items.value)
    delete this.items[value]
    return true
  }

  clear() {
    this.items = {}
  }

  size() {
    return Object.keys(this.items).length
  }

  values() {
    return Object.keys(this.items)
  }

  union(otherSet) {
    let isInstance = otherSet instanceof MySet
    if(!isInstance) return false
    
    let unionset = new Set()
    let values = this.values()
    for(let i = 0; i < values.length; i++) {
      unionSet.add(values[i])
    }
    values = otherSet.values()
    values.forEach(item => unionSet.add(item))
    
    return unionset
  }

//交集
  intersection(otherSet) {
    let isInstance = otherSet instanceof MySet
    if(!isInstance) return false
    
    let intersectionSet = new Set()
    let values = this.values()
    for(let i = 0; i < values.length; i++) {
      if(otherSet.has(values[i])) {
        intersectionSet.add(values[i])
      }
    }
    return intersectionSet
  }

//差集 A-B x属于A但是x不属于B
  difference(otherSet) {
    let isInstance = otherSet instanceof MySet
    if(!isInstance) return false
    
    let differenceSet = new Set()
    let values = this.values()
    for(let i = 0; i < values.length; i++) {
      if(!otherSet.has(values[i])) {
        differenceSet.add(values[i])
      }
    }
    return differenceSet
  }

//子集 A包含于B
  isContained(otherSet) {
    let isInstance = otherSet instanceof MySet
    if(!isInstance) return false
    
//     let isContained = true
    let values = this.values()
    for(let i = 0; i < values.length; i++) {
      if(!otherSet.has(values[i])) {
//         isContained = false
        return false
      }
    }
//     return isContained
    return true
  }
}

let newset = new MySet()
log(newset.union(2))

复制成功
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

数组的API-两个数组间的交集,补集,并集。

const log = console.log
//两个数组间的交集,补集,并集
const arr1 = [1,2,3,4]
const arr2 = [2,3,4,5,6]

const intersection = (arr1,arr2) => {
  //求交集,arr2用some.一旦两元素相等,filter就用真值
  let result = arr1.filter(item1 => arr2.some(item2 => item2 === item1))
  log(`交集`,result) 
  return result
} 
intersection(arr1,arr2) //[2,3,4]

const difference = (arr1,arr2) => {
  // 求补集, arr2用every,一旦arr2从没有这个元素,filter就用真值
  let result = arr1.filter(item1 => arr2.every(item2 => item1 !== item2))
  log(`补集`,result)
  return result
}
difference(arr1, arr2) //[1]

const union = (arr1, arr2) => {
  // 求并集,拿补集出来再和数组2合并
   // let result = difference(arr1, arr2).concat(arr2)
  let result = arr1.filter(item1 => arr2.every(item2 => item1 !== item2)).concat(arr2)
  log(`并集`,result)
  return result
}
union(arr1,arr2) //[1, 2, 3, 4, 5, 6]
复制成功
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
晓露寝安浅云逍遥十漾轻拟